Search code examples
c#unity-game-enginescene-manager

Is there a way to set a scene's content to a variable?


I have a game where I need to save a scene. I'm doing this by setting the scene to a variable which I can access later. The player then goes to a main menu when they are done.

The player now wants to go back to that scene, however, when it loads normally, it resets to its original state. Is there any way to use this variable to set the scene's contents to the variable I made earlier?

This is how I made the variable: Builder1 = SceneManager.GetActiveScene();

Any help would be greatly appreciated (and yes, I do realize that the variable I made probably doesn't store the scene the way I want it to). Thanks!


Solution

  • Scene is just a reference to the Scene object, and not all of the GameObject's locations/information at that moment in time. If the information in a scene shouldn't be lost yet, then it is advisable to not close the scene yet. Instead, take a look at Additive Scene Loading. You can addivitely load your new scene, switch your main camera to the new scene's camera, and when you are done there you can close the added scene and return the camera to the original scene.

    If you're looking to persist data between game loads, then you'll need to consider serializing all of the relevant data in your scene and build a data class to house all of it.