Object reference not set to an instance of an object


I keep getting the "NullReferenceException: Object reference not set to an instance of an object"

TempChunk = Instantiate(chunksG[2]) as GameObject;
Debug.Log("Temp Chunk Spawned");
Debug.Log("Temp Chunk Data Updated");

It errors after the first Debug.Log

chunksG is an array of prefabs TempChunk is a temporary variable where I make a new instance of a prefab and then puts it into an array to make a map

The prefab has a script called "ChunkScriptCS" which deals with the chunk.

I've been getting this error recently. The wording is a bit confusing, but Unity is just trying to tell you it can't find something that your code is obviously referring to. In this instance, I imagine it is the GetComponent part. I can't quite see why in your code, though, as there isn't much there.

You could try

TempChunk = (GameObject)Instantiate(chunksG[2]);

Also, try inserting the line:

Debug.LogAlert((TempChunk)?"Exists":"Does Not Exist!"); 

After the first line in the above example. If the log says "Does Not Exist", then try the line:

Debug.LogAlert((chunksG[2])?"Exists":"Does Not Exist!"); 

If it still says "Does not exist!" then you have a problem in your array. If it doesn't, then you have a problem in the line of code that creates the object.

This all assumes you are getting an error on the instantiation line, and not the line after it.