我是学习Unity3d游戏开发的新手,我有一个名为GameManager的GameObject,并且已将GameManager.cs添加到其中。像这样:
GameManager.cs 的片段,运行时出现空异常:
public void DisplayTileGrid() {
tiles = new List<MatchItem> ();
for (int x = 0; x < TileData.tileWidth; x++) {
for (int y = 0; y < TileData.tileHeight; y++) {
int type = (int)cells[x, y].cellType;
string spriteName = sprites[type - 1];
GameObject instance = NGUITools.AddChild(grid, matchItemPrefab) as GameObject;
instance.GetComponent<UISprite>().spriteName = spriteName;
instance.transform.localScale = Vector3.one * cellScale;
instance.transform.localPosition = new Vector3(x * cellWidth, y * -cellHeight, 0f);
MatchItem tile = instance.GetComponent<MatchItem>();
tile.target = gameObject;
tile.cell = cells[x, y];
tile.point = new TilePoint(x, y);
tiles.Add(tile);
}
}
}
似乎在这里添加我的 matchItemPrefab 失败:
GameObject instance = NGUITools.AddChild(grid, matchItemPrefab) as GameObject;
,并且 instance.GetComponent() 返回 null。
为什么实例对象没有 MatchItem?任何人都可以帮忙吗?