Object的参数不是Object的成员(UNITY EDITOR)

时间:2015-09-17 21:04:29

标签: unity3d unityscript

我有错误

  

Assets / TextPierwszy.js(22,28):BCE0019:'id'不是'Object'的成员。   Assets / TextPierwszy.js(24,38):BCE0019:'id'不是'Object'的成员。

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

<meta name="viewport" content="width=device-width, initial-scale=1" />
<nav>
  <ul>
    <li><a href="#">Item 1</a></li>
    <li><a href="#">Item 2</a></li>
    <li><a href="#">Item 3</a></li>
    <li><a href="#">Item 4</a></li>
    <li><a href="#">Item 5</a></li>
    <li><a href="#">Item 6</a></li>
    <li><a href="#">Item 7</a></li>
    <li><a href="#">Item 8</a></li>
    <li><a href="#">Item 9</a></li>
    <li><a href="#">Item 10</a></li>
  </ul>
</nav>

如何告诉编译器跟踪Ludnosc [0]作为Human的实例而不是在plain Object上跟踪它? 或者在其他地方有问题吗?也尝试过 #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 } }
但这也行不通。 :(

1 个答案:

答案 0 :(得分:2)

这是因为当您使用:

初始化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数组,则可以在Object中访问值时修改HumanTest1(),修改类型转换版本,然后放置它回到了数组中(没有测试过这段代码):

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);
    }
}

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