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 :frowning:
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 :smiley:

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!