Null Reference Exception, checked all inspector references

I am getting the ol’ null reference exception right after trying to hookup the WaveConfig Scriptable Object. Showing code first, then inspector screenshots.
WaveConfig.cs GetWayPoints() method

public List<Transform> GetWaypoints() 
    {
        var waveWaypoints = new List<Transform>();

        foreach (Transform child in pathPrefab.transform)
        {
            waveWaypoints.Add(child);
        }

        return waveWaypoints;
    }

EnemyPathing.cs Variable Declarations, Start(), Update(), and Move() methods

[SerializeField] WaveConfig waveConfig;
    List<Transform> waypoints;

    [SerializeField] float moveSpeed = 2f;

    int waypointIndex = 0;

    private void Start()
    {
        transform.position = waypoints[waypointIndex].position;
        waypoints = waveConfig.GetWaypoints();
    }

    private void Update()
    {
        Move();
    }


    private void Move()
    {
        if (waypointIndex <= waypoints.Count - 1) // Error points to this line
        {
            var targetPos = waypoints[waypointIndex].position;
            var movementThisFrame = moveSpeed * Time.deltaTime;

            transform.position = Vector2.MoveTowards(transform.position,
                                                     targetPos,
                                                     movementThisFrame);

            CheckArrival();
        }
        else
        {
            Destroy(gameObject);
        }
    }


I hope it’s something silly I’m missing!

Hi! Welcome to the community!

This is a common error in this section of the course, but somehow everyone manages to have the same error by doing something differently, but it usually has something to do with objects in the hierarchy or prefabs, check again and be sure everything is in order, if there’s no error there you’ll have to narrow the possibilities down I suggest using Debug.Log lines or print and read the console very carefully.

But first, compare your code with Rick’s, here’s the link to the GitHub commit.

Check which object is sending the message; Debug.Log(transform.name).

  • If the message comes from a single object then you probably have an enemy in the hierarchy that you were using as a template and for some reason, it isn’t updated.
  • If the message comes from every enemy spawned.
    • You’ll need to add more debug lines. Add this at the bottom of the Start method: Debug.Log(waypoints).
    • If it says null. There’s something wrong with your prefabs, try updating them again. Be sure that when you press play the Path prefab stays in the Scriptable Object.
    • If there’s a list:
      • At the top of your Move method add the same line as before Debug.Log(waypoints).
        • If it says null then somewhere in your code the list is being deleted or set to null, find out where and fix that issue.
        • If the list isn’t null then you’ll probably have to redo step 1 because there’s no chance the list is something and yet nothing at the same time, this isn’t Schrödinger’s code.
1 Like

I very, very much appreciate the quick and extremely helpful response. Funnily enough, just comparing my code to Rick’s was enough to fix the issue.

In my Start method, I used the waypoints variable before initialization. After switch the two lines in the method, it works completely fine!

Again, thank you for the step by step. It was a really big help to have the steps laid down in front of me, even something as simple as comparing code.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms