What is the best way to handle persistence in scene transitions?

Hey all. In my game, each room is its own scene. It’s like an old school Resident Evil game.

I need the player to move from one scene to the next and retain its current state. What is the best way to do this? I’m using DontDestroyOnLoad currently. However, even with a singleton this is causing some minor problems.

If I start in one scene, move to another, and then move back to the first scene, there are two copies of the player character and I haven’t found a reliable way to destroy the correct one.

Would it be better to just instantiate a player prefab every time a new scene loads (updating its state, inventory, etc.)?

For something like the player, an additive scene loading approach is probably the most tried-and-true method. Namely, have the player be it’s own scene that you additively load on top of the level scene (or scenes potentially). Transitioning to another scene is simply loading the next scene, moving the player, then unloading the previous scene.

This way, you don’t need to put the the player in any scene, or need to repeatedly instantiate them, or continually move data around. The scene is always there, thus everything that’s a part of it remains persistent.

1 Like

Thanks for the response. Unfortunately, I have no idea how to do that. I had a brief look on YouTube for a tutorial, but I didn’t find anything particularly useful.

Intuitively, I’m not sure I understand how one would move an object from one open scene to another open scene. Everything I’ve learned about Unity has been based on working from a single scene, with each scene having its own hierarchy.

The one video I found was a little over my head. The person seemed to have scenes for distinct purposes, like one dedicated to the player’s inventory.

You’re not moving things between scenes in the Unity sense, it’s about having multiple scenes loaded. The player character lives in its own scene that that stays loaded. It can still interact with objects in other scenes (namely physics/collision, raycasts, etc). Moving between areas is just loading the next scene moving the player game object over, then unloading the previous scene.

Having scenes for distinct purposes isn’t a bad idea, though something like an inventory can and usually should just be pure data.

1 Like