Unity3d ゲーム開発を勉強するのは初めてで、GameManager と呼ばれる GameObject が 1 つあり、それに GameManager.cs が追加されています。このような:
GameManager.cs の一部を抜粋すると、実行時に null 例外が発生しました。
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 がないのはなぜですか? 誰でも助けることができますか?