[Tilevania] MissingReferenceException: Gamesession destroyed

Good evening!
I’m currently on “Persistent Score & Lives” of the Tilevania course.
On the first level everything is processed fine. Score works, death works, reloading works all fine.

When I go to the second level and die, it gives me the error "Missign Reference Excepton: The object of type “GameSession” has been destroyed but you are still trying to access it. However I do have a Game Session object in my hierarchy.

When I doubleclick the error it leads me to the GameSession script but not to any particular lines.Any ideas?

(Note: I have moved all the reload stuff to Gamesessions but, as mentioned above, it used to work fine).

Thanks in advance!


This may actually be the problem. Perhaps you have a race condition. The components that reference GameSession may have obtained a reference to GameSession before the GameSession.Awake() was executed. They may, in this case, have obtained a reference to the scene’s GameSession instead of the one that’s in DDoL. In this case, their reference would get destroyed as soon as the Awake is executed.

Find the places where other components are getting a reference to GameSession. Make sure that they do not do it in their Awake methods. That might solve the problem.

PS: The reason it would work on the first level is because there is only one GameSession. As soon as the second level loads, there are 2, but one gets destroyed in the Awake.

Hey! It kinda kept me up at night and I came to a similair conclusion. Thank you for your answer.
In that case I wonder why there is another gamesession at all? Shouldn’t I just “carry over” the gamesession to the following level on each level after the first?

Could I use another empty object + script to manage the scenes? (I think it is usually done in a gamemanager?)

Usually you won’t have more game managers. But sometimes - especially when testing - you would put a game manager in the scene you are testing. Then it’s there when you start the game from the scene.

The DontDestroyOnLoad makes the object stay around when loading another scene. So, the one that loaded in the first scene is marked as DDoL, but you also have the one that is in the hierarchy already. This one destroys itself when it finds the original.

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

Privacy & Terms