WaveConfig Null Reference Exception

Working through video #97 and I got a NullReferenceException on the waypoints=waveConfig.GetWaypoints(); line in the EnemyPathing.cs start method:

void Start () {

        waypoints = waveConfig.GetWaypoints();
        transform.position = waypoints[waypointIndex].transform.position;

	}

To troubleshoot, I added a
Debug.Log(waveConfig);
immediately before the erroring line. Then it started working and continued to work after I took out the Debug.Log(waveConfig) line.

I haven’t gotten it to repeat the error again, but does anyone have any thoughts on why this may have been happening?

This would happen if you don’t have a WaveConfig for that enemy. Since its the spawner that gives the Enemy the WaveConfig, any enemies that start in the scene won’t have a WaveConfig and will get this error.

In the videos, Rick has a little Roger starting in the scene and he later removes it when he notices this error. It won’t keep your game from running - it will just keep that enemy from moving (because they have no WaveConfig data).

2 Likes

Thanks so much for sharing Anthony because I was getting the same error and didn’t know why. I deleted that enemy I had on starting scene and now I am not getting that error anymore.

If you want to take on a challenge…

Now that you know what causes the error and why, there are a couple of ways you could make sure its impossible for that condition to cause that error again.

Can you find a way to make your “dangerous” code safer?

1 Like

I’m not sure. It’s a challenge right now just for me to remember the things Rick is teaching us. Maybe later down the road when I am more comfortable with the coding. I have tried things with the previous games and ended up with a mess so I’m a little paranoid now to mess around with things until I learn more.

1 Like

Feels like there is a race condition here.
We’re spawning an enemy without using a constructor to immediately specify a critical piece of information (i.e. the waveConfig). The enemyPathing script’s start method immediately accesses this information. As this is written, how can we be sure that the EnemySpawner script will call SetWaveConfig before the start method in EnemyPathing fires?

Per an earlier comment, wouldn’t it be safer to pass this information in on instantiation?
Not sure how to do this with Unity GameObjects yet, but in programming it would be using a constructor.

Hi, Paul. There is no race condition here. SetWaveConfig is being run as part of the same Update() that spawned the Roger. Since all Start() functions are run before Update(), it won’t run it’s Start() until next frame. The issue would be different if we had used Awake() instead.

2 Likes

I have a similar problem, which got better after I deleted the Enemy from the scene. Originally 3 errors, now just left with this one. Help!

image

startingwave is null? Maybe you needto drag a waveconfig into the startingwave field in the Unity inspector.

1 Like

sorry, whereabouts? I don’t see that field anywhere… also I just started getting this message rather than the exception. Appreciate the help buddy…

image

Can you post the inspector for your EnemySpawner. Specifically, I’d like to see how waveConfigs is setup.

1 Like

Holy c***, there it is! Couldn’t see for looking :scream:
Thanks so much for the help man, lifesaver. I was about to give up and start playing Skylines again :grimacing:
Just gave it a value and added a Wave, hey presto!
I owe you one buddy :partying_face:

i need help

Thanks, Man!

Privacy & Terms