2015-09-17 86 views
0

我有錯誤對象的參數不是對象(UNITY EDITOR)的成員

資產/ TextPierwszy.js(22,28):BCE0019:「身份證」不是「對象」中的一員。 Assets/TextPierwszy.js(24,38):BCE0019:'id'不是'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]人的實例,而不是在普通對象跟蹤呢? 或者在其他地方有問題嗎?也試過
public var Ludnosc : Human = new Array();
但這不起作用。 :(

回答

2

這是因爲當你使用初始化Ludnosc

public var Ludnosc = new Array(); 

你創建Object元素的數組作爲結果,當您嘗試訪問Ludnosc[0].idLudnosc[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,修改typecasted版本,然後將其放回數組中(未測試此代碼):

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

希望這有助於!如果您有任何問題,請告訴我。

+0

非常感謝!第二種解決方案工作正常,因爲我需要_unlimited_數組,當我想爲更多的人獲得更多的「空間」時,我不需要重新初始化。 –

+0

太棒了!我很高興我能幫忙。 – Serlite