Getting a simple error in my navigation scripting but can't tell why - NullReferenceException

So building a tower defense game and needed to use navMesh for enemy pathing since the enemies need to path around new things, not just along a completely set path. Anyway, since I’ll need to instantiate enemies I can’t use public variables for the waypoints, as such I made a waypoints script to index them. It works fine as far as I can tell and is this:

using UnityEngine;

public class Waypoints : MonoBehaviour
{

    public static Transform[] points;

    void Awake()
    {
        points = new Transform[transform.childCount];
        for (int i = 0; i < points.Length; i++)
        {
            points[i] = transform.GetChild(i);
        }
    }

}

So that’s attached to the parent of the waypoints.

Now I make my Enemy Navigation ai and it’s able to navigate to the first point, then gets stuck there. Right as the enemy is instantiated it has a NullReferenceException:
“NullReferenceException: Object reference not set to an instance of an object
NewNav.Awake () (at Assets/Scripts/NewNav.cs:19)”

The script is telling me it can’t reference the first waypoints script, though it still finds the first point from that same waypoints script, am I incorrect?

Here’s the navigation script that’s giving me the NullReferenceException:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class NewNav : MonoBehaviour
{

    private Transform target;
    private int wavepointIndex = 0;
    private NavMeshAgent agent;



    void Awake()
    {
        agent = GetComponent<NavMeshAgent>();

        target = Waypoints.points[0];

        // Disabling auto-braking allows for continuous movement
        // between points (ie, the agent doesn't slow down as it
        // approaches a destination point).
        agent.autoBraking = false;

        GotoNextPoint();
    }


    void GotoNextPoint()
    {

        // Set the agent to go to the currently selected destination.
        agent.destination = target.position;

        // Choose the next point in the array as the destination,
        // cycling to the start if necessary.
        wavepointIndex = wavepointIndex++;
        target = Waypoints.points[wavepointIndex];
    }


    void Update()
    {
        // Choose the next destination point when the agent gets
        // close to the current one.
        if (!agent.pathPending && agent.remainingDistance < 0.05f)
            GotoNextPoint();
       
    }
}

If anyone can enlighten me on what it is I’m missing here I’d greatly appreciate (and likely feel like an idiot - I assume this is a stupid error or an easy fix and I’m just 10 years too rusty to be doing this)

Thank you!

You can find out!!

The answer is always the same… ALWAYS. It is the single most common error ever.

Don’t waste your life spinning around and round on this error. Instead, learn how to fix it fast… it’s EASY!!

Some notes on how to fix a NullReferenceException error in Unity3D

  • also known as: Unassigned Reference Exception
  • also known as: Missing Reference Exception
  • also known as: Object reference not set to an instance of an object

http://plbm.com/?p=221

The basic steps outlined above are:

  • Identify what is null
  • Identify why it is null
  • Fix that.

Expect to see this error a LOT. It’s easily the most common thing to do when working. Learn how to fix it rapidly. It’s easy. See the above link for more tips.

This is the kind of mindset and thinking process you need to bring to this problem:

https://discussions.unity.com/t/814091/4

Step by step, break it down, find the problem.

The first problem that comes to my mind is that you define Waypoints.points in an Awake function in a different script, and try to acces it in this one also in Awake

now, this would work fine if Waypoints.Awake() is always executed befor NewNav.Awake(), tho as it is right now you are not ensuring that…

lets just assume that NewNav.Awake() gets executed befor Waypoints.Awake():
than Waypoints.points is null
and in line 19 in Waypoints.Awake() you try to acces the first element in Waypoints.points, tho as we already established, Waypoints.points is null at that point, and you cant get the first element of something that does not exist yet…

one solution would be to change the “Awake()” in NewNav to “Start()” since the MonoBehaviour Start functions are always executed after everything had a chance to fully executed Awake