- Home /
 
               Question by 
               warrior_rui · Mar 22, 2015 at 11:14 PM · 
                c#getcomponentnullreferenceexeption  
              
 
              C# about NGUITools.AddChild and GetComponent Null
I am new to study Unity3d game development, I am following this open source demo and design the similar game. https://github.com/textcube/match3action.git
I have one GameObject so called GameManager, and GameManager.cs has been added to it. Like this: 
Here is the snip of GameManager.cs, I got null exception when I run:
 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);
             }
         }
     }
Seems it is failed to add my matchItemPrefab here:
 GameObject instance = NGUITools.AddChild(grid, matchItemPrefab) as GameObject;
, and instance.GetComponent() returned null.
Why doesn't instance object have MatchItem? Can anyone help?
 
                 
                screen-shot-2015-03-21-at-10300-pm.png 
                (58.5 kB) 
               
 
              
               Comment
              
 
               
              Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                