对象的参数不是对象的成员(UNITY EDITOR)

Object's parameter is not a member of Object(UNITY EDITOR)

我有错误

Assets/TextPierwszy.js(22,28): BCE0019: 'id' is not a member of 'Object'. Assets/TextPierwszy.js(24,38): BCE0019: 'id' is not a member of 'Object'.

尝试在 UnityScript 中编译该脚本时。

#pragma strict
private var pole : UI.Text;
public var Started = false;

public var Ludnosc = new Array();

public class Human {
    public var id : byte;
    public var gender : byte; // 0=k 1=m
    public var age : byte;
    public var pregnant : byte;
    function Breed(partner) {
        // Tu będzie logika rozmnażania
    }
    public var parents : int[]; //Najpierw podajemy ID matki, potem ID ojca.
}

function Test1() {
    if(!Started) {
        Started = true;
        Ludnosc.push(new Human());
        Ludnosc[0].id = 1; //Line number 22
        Debug.Log(Ludnosc.length);
        Debug.Log(Ludnosc[0].id); //Line number 24
        }
}

我如何告诉编译器将 Ludnosc[0] 作为 Human 的实例进行跟踪,而不是在普通对象上进行跟踪? 还是其他地方有问题?也尝试过
public var Ludnosc : Human = new Array();
但这也行不通。 :(

这是因为当您初始化 Ludnosc 时使用:

public var Ludnosc = new Array();

您正在创建一个包含 Object 个元素的数组。因此,当您尝试访问 Ludnosc[0].id 时,Ludnosc[0] 被视为 Object,因此没有可用的 id

要解决此问题,请将 Ludnosc 初始化为内置数组,如下所示:

public var Ludnosc : Human[];

Ludnosc = new Human[1]; // When you're initializing it
Ludnosc[0] = new Human(); // When you're populating it

或者,如果你真的想使用JavaScript数组,你可以在访问Test1()中的值时将Object转换为Human,修改类型转换后的版本,然后将其放回数组中(尚未测试此代码):

function Test1() {
    if(!Started) {
        Started = true;
        Ludnosc.push(new Human());
        var tempHuman = Ludnosc[0] as Human;
        tempHuman.id = 1;
        Ludnosc[0] = tempHuman; // Overwriting with the updated Human
        Debug.Log(Ludnosc.length);
        Debug.Log(tempHuman.id);
    }
}

希望对您有所帮助!如果您有任何问题,请告诉我。