2016-01-23 179 views
0
for (int x = 0; x < mapLength; x++) 
    { 
     var instantiateMap = new Vector3(x * 2, 0, 1); 
     GameObject[] cubeObjectClones = Instantiate(cubeObject, instantiateMap, Quaternion.identity) as GameObject; 
     cubeObjectClones[x].transform.parent = transform; 
    } 

無法隱式轉換類型UnityEngine.GameObject' to UnityEngine.GameObject []」遊戲對象[]在Unity

一些能解釋一下我可以修復這個錯誤?

回答

5

這是因爲Instantiate方法返回一個單一的遊戲對象。相反,您需要使用循環來生成更多實例並將其添加到您的數組中。使用下面的代碼。

GameObject[] cubeObjectClones = new GameObject[mapLength]; 
for (int x = 0; x < mapLength; x++) 
{ 
    var instantiateMap = new Vector3(x * 2, 0, 1); 
    cubeObjectClones[x] = Instantiate(cubeObject, instantiateMap, Quaternion.identity) as GameObject; 
    cubeObjectClones[x].transform.parent = transform; 
} 
相關問題