获取组件空值

GetComponent Null

刚开始学习Unity3d游戏开发,我有一个GameObject,叫GameManager,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() 返回空值。

为什么实例对象没有MatchItem?有人可以帮忙吗?

我发现根本原因是我没有将 MatchItem.cs 添加到我的预制件中,现在 null 异常消失了。