"GameOver" scene refuses to load after player dies

My code is the same as Rick’s (I do that so that I can track errors with his help more easily), but the difference is that in this lecture, after his player is dead, the game over scene is loaded, however in my case my player dies and “GameScene” still continues with all the enemies shooting and flying around, even after 2 seconds meant for the “scene-switching” delay pass.

Here is the Level.cs code (just so that we’re sure I’m not missing something):

Level%20code%20ss

And here is Player.cs:

And here, is the error I’m getting:

When I double click the error, it takes me to Player.cs and highlights the Die() method. I tried rearranging the statements, e.g. putting the FindObject statement above Destroy(gameObject) statement, and certain things do change (If I put FindObjectOfType<Level>.LoadGameOver(); above the Destroy(gameObject) statement, the player doesn’t even die, for example - if you believe this arrangement of statements is important tell me, but I do not think it matters, because if my code is the same as Rick’s I don’t think the problem is in the code at all, though I might be wrong).

In desperation I even updated the unity editor (thinking that this, perhaps, bug, is fixed in the latter versions) and I hardly ever do that. Help would be greatly appreciated! I don’t know what to do any more with this goddamn engine :thinking: :thinking: :thinking:

2 Likes

Hi čedomir,

There are a few reasons which could cause your issue, but looking at your description of how the error points to your Die method we need to look for things which could be null.

The first line of your method attempts to call the LoadGameOverScene but is using a Find method in order to do so. It is making a huge assumption that an object of type Level will be returned. If the object wasn’t returned by this method, you would then be trying to call LoadGameOverScene on a null reference, which would certainly cause your error.

The second line only needs a reference to the GameObject the script is a component of, as your script is erroring, it is fairly safe to assume that the gameObject is not null at this point.

On the third line you use the static method PlayClipAtPoint which does not need a reference to an object, other than the deathSFX AudioClip which you pass in to it.

So, in summary, there are two potential areas of issue;

  • you either don’t have a GameObject in your scene with a Level script component attached
  • you haven’t created the reference for the deathSFX

I suspect it will be the first. Check you scene and look for the GameObject, originally named “Level” which should have a Level script component attached to it. If there isn’t one, add one.

Regarding the updating of Unity, this was perhaps a little bit desperate, the error message is pointing to your code, not Unity’s - but I can appreciate your frustration and desire to try anything to resolve the issue.

On a related note, your error messages and details above are very useful, but the screenshots of your code are not. There is no correlation to the line numbers you mention from the error messages. Screenshots are best used for error messages and details from within the Unity editor. For code, please copy/paste your code into your posts and then apply the code formatting characters bother before and after it (see link below).

I hope this helps :slight_smile:


See also;

1 Like

Yes your suspecting was true. I didn’t put a Level game object into my scene, and I guess I skipped through the part of the video in which Rick does it.

I hope I have not made it harder for you to see what the problem might be by trying to post my code here in an inappropriate way, and if I have, I’m sorry. I will from now on do as you said, regarding the manner of sharing my code.

Thanks a lot anyhow! Everything is in order at the moment. Have a nice day :smile::v:

1 Like

HI čedomir,

No need to apologise, the notes above about copy/pasting your code are as much for your benefit, to be honest, the easier it is for people to see the issue and respond, the greater the likelihood that people will :slight_smile:

I’m glad that you’ve managed to resolve the issue and can move forward with the course again, and you are very welcome :slight_smile:

1 Like

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

Privacy & Terms