- Home /
Null Reference Exception Error
I am getting the following error:
(wrapper stelemref) object:stelemref (object,intptr,object) Game.SpawnCharacter (Int32 ID, System.String Name, Int32 iX, Int32 iY, System.String Owner, Int32 MR) (at Assets/Game/Game.cs:130) Game.OnExtResponse (Sfs2X.Core.BaseEvent e) (at Assets/Game/Game.cs:123) I've triple checked everything relating to these segments of code and nothing seems to be un-instantiated. So I'm at a loss to what it can be. Here is the code: public TileData[,] Tile = new TileData[16, 16]; public TileData ActiveTile = new TileData(); public CharacterData[] Character = new CharacterData[25];NullReferenceException: Object reference not set to an instance of an object
...
void OnExtResponse(BaseEvent e) { string cmd = (string)e.Params["cmd"]; ISFSObject ObjInput = (SFSObject)e.Params["params"];
if (cmd == "Start Match") { Debug.Log("Received Message"); for(int i = 0; i < ObjInput.GetInt("CharacterTotal"); i++) { Debug.Log("Spawning Character"); SpawnCharacter(i, (string)ObjInput.GetUtfStringArray("CharType").GetValue(i), (int)ObjInput.GetIntArray("xPos").GetValue(i), (int)ObjInput.GetIntArray("yPos").GetValue(i), (string)ObjInput.GetUtfStringArray("Owner").GetValue(i), (int)ObjInput.GetIntArray("MovementRange").GetValue(i)); //Error is here } UpdateChat(ObjInput.GetUtfString("TurnNotice")); } } void SpawnCharacter(int ID, string Name, int iX, int iY, string Owner, int MR) { Character[ID] = new CharacterData(); Character[ID].CharacterOBJ = new GameObject(); //Error occurs with our without this line Character[ID].CharacterOBJ = GameObject.Instantiate(Resources.Load(Name)) as GameObject; Character[ID].iX = iX; Character[ID].iY = iY; Character[ID].Owner = Owner; Character[ID].MovementRange = MR; Vector3 Pos = new Vector3((float)0.5 + iX, 0, (float)15.5 - iY); Character[ID].CharacterOBJ.transform.position = Pos; if(iY > 7) { Vector3 rot = new Vector3(0, 180, 0); Character[ID].CharacterOBJ.transform.Rotate(rot); } if(Character[ID].Owner == Username) { Character[ID].IsLocallyOwned = true; } if (Character[ID].Owner != Username) { Character[ID].IsLocallyOwned = false; }
Tile[iX, iY].IsOccupied = true; Tile[iX, iY].OccupierID = ID; }
What line is generating the error. You are giving us a cut-down version of the class, so we cannot map the error to a specific line in the code.
It is in there, the really long line that you need to scroll to see. It's Line 18 here
That is a very noisy line. Recommend breaking it down into several lines which you can analyze in the visual debugger.
Or, if you prefer the console print route:
Debug.Log((string)ObjInput.GetUtfStringArray("CharType").GetValue(i));
Debug.Log((int)ObjInput.GetIntArray("xPos").GetValue(i));
Debug.Log((int)ObjInput.GetIntArray("yPos").GetValue(i));
Debug.Log((string)ObjInput.GetUtfStringArray("Owner").GetValue(i));
Debug.Log((int)ObjInput.GetIntArray("$$anonymous$$ovementRange").GetValue(i)));
If one of those lines errors out, you'll at least have a better idea of what went wrong.
Answer by Gabe_ · Aug 08, 2014 at 01:11 AM
I solved it myself, turns out having an empty GameObject, such as public TileData ActiveTile = new TileData(), caused the error...
Weird how it listed a seemingly arbitrary line for the error code.
Your answer