Help: I'm getting a NullReferenceException error which I can't figure out

Hi!

I’m coming to the end of the laser defender part of the 2D course (video 115) & I’m at the point where I can do a full loop of the game. However, when I start the game and go from the start menu to the game scene, I get the NullReferenceException error in my console.

When I double click it, it points me to line 19 of my Level.cs script (pictured). I can’t figure out what it’s asking me to do & I’m not sure why I’m getting the error. The game works fine regardless, but I’d like to figure out a solution to get rid of the error if possible.

If anyone can guide me, I’d be super grateful!

Thanks :slight_smile:

I noticed Rick gets the same NullReferenceException error I’m getting, however he said he just cleared the console & played the game a few times & the errors didn’t come back. I don’t understand how clearing the console would stop the error from happening as the console only provides feedback & doesn’t actually update or fix the code. I’ve looked up why NullReference Exception occurs & it seems like I’m trying to reference something that doesn’t exist or hasn’t been initialized.

The error is coming at the point where the coroutine which resets the game runs. I thought a coroutine was just a block of code which runs when certain conditions are met? I’m so confused as to how a NullReferenceException would be relevant to a coroutine :frowning:

For anyone curious, I have managed to solve the error I was getting.

It seemed like the error showed up only when I loaded the game scene from the main menu for the first time. As the problem seemed to be coming from the coroutine (and the coroutine is only required when we need to reset the game) I figured that calling the ResetGame(); method with the coroutine in the first instance where we load the game was probably a bad idea.

My solution was to have two methods that load the game:

1: LoadGameSceneFirst();
and
2: LoadGameScene();

LoadGameSceneFirst(); would be the method I use to load the game scene the very first time from the start menu. This method would not contain the coroutine which ran the ResetGame(); method. When I tested it, I was able to start the game without any errors in the console.

Of course, I still needed a way to reset the game once the player got a Game Over by destroying the current GameSession. This is when I would use the LoadGameScene(); method. This method also loads the game scene, but because it contains the coroutine (which destroys the current GameSession), we are able to load a new GameSession along with the Game scene. This means that the players score is reset back to 0 & can be recalculated.

I’m still not sure exactly what the issue was, so if anyone can help me understand it, I would be very grateful. I think it has something to do with a conflict between the very first GameSession & the method that destroys the current GameSession contained within the coroutine. Either way, I’m proud of myself for solving the issue & hopefully this may help anyone else who might encounter the same issue.

Adios :slight_smile:

Good job on fixing the problem, and thanks a lot for sharing your solution! :slight_smile:

1 Like

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