How to preserve scene state between lives lost?

With the introduction of the Game Session that re-loads the active scene on death, I have lost the ability to keep the level state the same between player deaths.

Notably I have state on the player (last checkpoint), and in-game triggers (things player does that affect the contents of tilemaps) that I want to preserve between deaths. Other things make sense to reload (such as enemies.)

I have already tried things like setting DontDestroyOnLoad on gameobjects I wish to preserve, then adding back to the current scene after it loads (using LoadSceneAsync and yielding until it is done), before moving the object back to the active scene and destroying the new copy from the load, but it just doesn’t work.

Any suggestions? Thanks!

I’m also curious when to use this pattern of Singleton (attaching the script on a game object in every scene, destroying dupes) vs using a classic static instance of the script.

So I got to a later lecture introducing Scene Persist, which I could kind of work with, but pretty much everything had to be added! To keep player state persisted, I added that as a child of the Scene Persist object, but then after death the cameras lost their reference to the player for the “Follow” field. So I added cameras to scene persist, but after death, they lost their reference to the background collider for the confiner so I added the tilemaps to scene persist. Finally I had pretty much everything in scene persist except for my enemies which I wanted to reset between lives lost lol.

Hi,

If other game objects are supposed to find your active “singleton” object during runtime, the approach with FindObjectOfType is probably safer because the static instance approach does not take the state (active, inactive) of the object into consideration.

You do not have to make everything persistent. With FindObjectOfType, you could look for certain objects during runtime, for example, after a scene was loaded.

Thanks. The first part makes sense to me as to why to use this pattern. I don’t understand what you are implying I do in your second point, is it that I somehow swap out the GameObject’s state with the state from before the reload in that event?

Assuming you have a GameManager object in level 1. You make that object persistent. It does not exist in level 2, so you cannot assign it to any game objects in level 2 manually. When you play your game and load level 2 from level 1, the GameManager object “suddenly” exists in level 2. Your game objects that did not exist in level 1 might need to look for an active GameManager object in your scene (level 2). That can be achieved with FindObjectOfType, which returns an active (not disabled!) object in your Hierarchy.

This potential solution is referring to the following but maybe I misunderstood your problem.

I have already tried things like setting DontDestroyOnLoad on gameobjects I wish to preserve, then adding back to the current scene after it loads (using LoadSceneAsync and yielding until it is done), before moving the object back to the active scene and destroying the new copy from the load, but it just doesn’t work.

Thanks again for replying, I think the confusion was because I was thinking of the case in this tutorial where the same scene was reloaded (not going from level 1 to 2, but loading 1 again while dying for example.) Either way I ended up getting things working, but I appreciate the help!

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

Privacy & Terms