Null Reference Exception in Section 6 Lecture 99


This is the error that Unity shows me but I don’t know what can I do

This is the part of my code that shows the error and that method is used in this other script.

Finally I let you my script from ‘enemySpawner’ but I don’t think that the problem is there.

I’ve been stock in here one hour! I hope you guys can help me!
The problem here is that the enemy doesn’t move to the next waypoint.

Forget it, my enemy prefab didn’t have a wave config in his [SerializeField], I just added and work.

1 Like

Edit: Oh, posted at the same time, I’ll leave this here anyway, you might find it useful.

The error is basically telling you that your list is empty. This type or errors are kinda hard to solve remotely, but I’ll try to give you some tools for you to be able to manage this kind of errors more easily.

When having this exceptions what you’ll need to solve them is information. First thing first, check if your variable WaveConfig has been set correctly in the Editor. Now let’s code some way to get all the information you’ll need to solve the issue. This is fairly easy:

void Start()
{
     print(gameObject.name); // 1 (Explanation below)
     print(waveConfig); // 2
     
     waypoints = waveConfig.GetWaypoints();
     print(waypoints.Count); // 3
     
     transform.positions = waypoints[waypointIndex].transform.position;
}

1.- If your console prints multiple names it means you have several game objects with the script attached, search for those objects and remove the script from unwanted places or set the variable “waveConfig” in the editor for those scripts as well.

2.- This is just to be sure that the variable is being set correctly and you are not trying to access a “null”.

3.- Let’s be sure your list isn’t empty. If it is then there’s an issue with your method that returns the list, which I think is not, since you are accessing nothing according to your console.

This will lead to a breamcrum trail, follow it and you’ll be able to find your error.

1 Like

thank you!!

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

Privacy & Terms