- Home /
Object reference not set to an instance of an object.
Upon starting my game, I receive this error in the console:
NullReferenceException: Object reference not set to an instance of an object
Terrain_Generator.Start () (at Assets/Terrain_Generator.cs:28)
It says the prevRow object reference is not set even though it should be set in the code below:
using UnityEngine;
using System.Collections;
public class Terrain_Generator : MonoBehaviour
{
public GameObject Target_Obj;
public int width;
public int length;
public int height;
int prevPos;
int horizPos;
GameObject prevRow;
int prevRowPos;
int prevHigh;
int prevLow;
void Start ()
{
for (int j = 0; j < width + 1; j = j + 1)
{
for (int i = 0; i < length + 1; i = i + 1)
{
prevRowPos = j - 1;
horizPos = Random.Range (prevPos - height, prevPos + height + 1);
if(j >= 1)
{
prevRow = GameObject.Find ("Cube (" + i + "," + prevRowPos + ")");
if(horizPos > prevRow.transform.position.y)
{
horizPos = (int)prevRow.transform.position.y + 1;
}
if(horizPos < prevRow.transform.position.y)
{
horizPos = (int)prevRow.transform.position.y - 1;
}
}
Object.Instantiate (Target_Obj, new Vector3 (j, horizPos, i), Quaternion.identity);
prevPos = horizPos;
}
}
}
}
If it is created from Instantiate and a prefab then it is named with a (Clone) extension.
Well then, as long as the rest of the code gets assumed, we can only assume a solution. Posting all the relevant code would probably fix this in a $$anonymous$$ute.
Answer by LessThanEpic · May 22, 2015 at 01:13 AM
GameObject.Find can return null. Have you tried printing out what the result of
"Cube (" + i + "," + prevRowPos + ")"
is and verified that you have an active game object of that name when that code runs?
I have verified that an active game object with this name does exist.
Active Object: Cube (0,0)
Attempted reference: Cube (0,0)
Still returning null.
Am currently using Unity version 4.5.4f1, if that information is useful.
When does your cube get created? You're trying to find it in the Start method and if it's being created by another script then the other script may not have had a chance to run yet.
The cube is created in this script, the Target_Obj object is referencing the cube prefab, which has a script that automatically names it once it is created based on it's x and z positions.
Perhaps it hasn't had a chance to name it... I might try sticking a delay in the code somewhere.
Yeah, the script on the new game object probably hasn't had a chance to run yet. You could get rid of the other script and rename the game object in this script. Something like:
GameObject newCube = (GameObject) Object.Instantiate (Target_Obj, new Vector3 (j, horizPos, i), Quaternion.identity);
newCube.name = //whatever you want to name it goes here
That might just be the ticket, I've been wondering how to reference an object that was just instantiated. Will test shortly.
Answer by p3k07 · May 22, 2015 at 05:17 AM
try: Object metaTarget = Instantiate(Target_Obj, new Vector3 (j, horizPos, i), Quaternion.identity);
or GameObject instead of Object.