Unity3d数组索引超出范围

时间:2015-11-05 10:04:04

标签: c# unity3d

void FillGrid()
{ 
    for(int x = 0; x < xSize; x++)
    {
        for(int y = 0; y < ySize; y++)
        {
            InstantiateCandy(x, y);
        }

    }
}
void InstantiateCandy(int x,int y)
{
    GameObject randomCandy = _candies[Random.Range(0, _candies.Length)];

    GameObject newCandy =(GameObject)Instantiate(randomCandy, new Vector3(x, y), Quaternion.identity);
    newCandy.GetComponent<GridItem>().OnItemPositionChanged(x, y);


}
void GetCandies()
{
    _candies = Resources.LoadAll<GameObject>("Prefabs");

    for(int i=0; i< _candies.Length-1;i++)
    {
        _candies[i].GetComponent<GridItem>().id = i;
    }

}


}

我的问题在这一行:

GameObject randomCandy = _candies[Random.Range(0, _candies.Length)];

我无法修复它。有人可以帮我解决吗?

1 个答案:

答案 0 :(得分:2)

_candiesGetCandies()中初始化,但在您的代码中未调用此函数。我怀疑,当执行有罪的行时,_candies仍为空。