Null Reference Exception

When attempting to play the game, an error pops up telling me the following;

“NullReferenceException: Object reference not set to an instance of an object
CompleteProject.EnemyMovement.Update () (at Assets/_CompletedAssets/Scripts/Enemy/EnemyMovement.cs:27)”

What does that mean?

This is the script:

using UnityEngine;
using System.Collections;

namespace CompleteProject
    public class EnemyMovement : MonoBehaviour
        Transform player;               // Reference to the player's position.
        PlayerHealth playerHealth;      // Reference to the player's health.
        EnemyHealth enemyHealth;        // Reference to this enemy's health.
        NavMeshAgent nav;               // Reference to the nav mesh agent.

        void Awake ()
            // Set up the references.
            player = GameObject.FindGameObjectWithTag ("Player").transform;
            playerHealth = player.GetComponent <PlayerHealth> ();
            enemyHealth = GetComponent <EnemyHealth> ();
            nav = GetComponent <NavMeshAgent> ();

        void Update ()
             // If the enemy and the player have health left...
            if(enemyHealth.currentHealth > 0 && playerHealth.currentHealth > 0)
                // ... set the destination of the nav mesh agent to the player.
                nav.SetDestination (player.position);
            // Otherwise...
                // ... disable the nav mesh agent.
                nav.enabled = false;

Null Reference means that you try to access a variable that holds a null value.
The last number: “nt.cs:27)” => 27 means at what line does it happens.
Looking at your code, line 27 would be:

if(enemyHealth.currentHealth > 0 && playerHealth.currentHealth > 0)

Here you’re accessing two variables, enemyHealth and playerHealth in the same line, so I can’t really tell which one has a null reference. Either way, the cause is in the Awake() method. What’s happening is that when Awake() is called, either (or both) of those don’t exists yet.

Try changing Awake() for Start(), that should fix it.

Why would that work?
Read about excecution order, basically, Awake() is called when a GameObject is created, so others might not exists just yet. Start() is called a little bit later, when all GameObjects are created, so you CAN find them.