- Home /
NullReferenceException: Object reference not set to an instance of an object
I was working on a game and I was confronted by this error: (NullReferenceException: Object reference not set to an instance of an object) and it's completely stopped my progress and I can't find any answer that works for me :( Here's the code.
void Update ()
{
if (NavMesh.CalculatePath (EnemySpawn.position, Core.position, NavMesh.AllAreas, path))
{
if (BuildMode == true && Input.GetKeyDown (KeyCode.Return))
{
StartCoroutine (StartGame (SpawnInterval));
}
}
else
{
print ("No Path Found");
}
}
It says the problem is in this line:
if (NavMesh.CalculatePath (EnemySpawn.position, Core.position, NavMesh.AllAreas, path))
All help is greatly appreciated :D
If you read closely the error message it indicates you the line and the word in conflict. You have an object reference that is null, never initialized. Tricksman response will definitly help you figure wich variable is null :D
Answer by Trickman · Aug 19, 2016 at 12:30 PM
The NullReferenceException indicates that you haven't given a value or initialized one (or more) of the objects which you're using in the operation.
Do this before the line that's giving problems:
Debug.Log("EnemySpawn = " + EnemySpawn);
Debug.Log("Core = " + Core);
Debug.Log("NavMesh = " + NavMesh);
Debug.Log("path = " + path);
... And run the game. Look at the console, the value or values displayed as "null" are the ones that you haven't initialized. Give them a value in the Start() function or through the editor.
Hope it helps!