SetWaveConfig() and Null Reference Exception

In Lecture 100. WaveConfig For Path&Speed, where Rick shows us how to set wave configuration for an enemy within the EnemySpawner script, we create this enemy object which requires a wave config scriptable object the moment it’s created. However, the assignment of this scriptable object won’t happen until the next line, therefore, we would get this null reference exception in the EnemyPathing script attached to the enemy object. I thought this should the reason I was getting the exception, but WHYYYYYY, Rick had the same setup, but he wasn’t encountering any error and it worked. I looked at other posts and they didn’t help. This is really frustrating, please someone explains this to me, please.

This is the EnemySpawner script, as you can see, in line 30 we instantiate an enemy object, in the next line we assign a wave config to it. But as it runs, we get this null reference expcetion:

NullReferenceExcpetion

I looked at the EnemyPathing script:

In line 16 and line 33 it requires a wave config, but since at this moment, line 31 in EnemySpawner hasn’t been run yet, we get the null reference excpetion.

1 Like

You are working on a single thread, this means that the loop will end before any of the Enemies’ Start method executes, that’s not causing your problem, there’s something else, by seeing your code it probably has something to do with that waveConfig exposed list, you probably forgot to add the Scriptable Objects in the inspector.

Hi Yee, thank you for your answer. The single thread thing does make sense, but I didn’t forget to add wave configs in the inspector.


What else do you think can be the issue? Thank you.

1 Like

I compared your code to Rick’s and I didn’t see any difference other than some methods named differently and I don’t think those scripts are linked to any other.

We’ll need to track the error down, try these ideas and see if this helps:

  • Are the enemies spawning and moving? If that’s the case then you have an extra Enemy Spawner script somewhere. To track that down just right-click on the script and select the Find References In Scene option.

  • Add a Debug Line to the SetWaveConfig method and see if it is being called correctly.

  • Add a Debug Line to the SetWaveConfig method and see if it is sending the correct parameter.

  • Add a Debug Line to the SpawnAllEnemiesInWave coroutine and see if it has the correct parameter.

If none of those help you track down the bug, then I’ll ask you to upload your game as a Unity Package and put a download link here so I can check your game.

Thank you for your help. It was a silly mistake. There was an enemy object in the scene. So the exception didn’t come from the spawned enemy but from that one. Sorry, such a silly mistake .

1 Like

Hi @Tim_Z,

I am glad that Yee was able to help you fix the problem. :slight_smile:


See also:

2 Likes

Yes, this community’s help is the reason that I haven’t quit because programming sometimes is really frustrating. So I’m really thankful for all the obliging wonderful people in this community. :slightly_smiling_face:

1 Like

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

Privacy & Terms