How to keep game states while loading different scenes,How to keep game states while changing scenes


In my game, the Player must collide with the object tagged “Next” in order to load the next level. Game state should = Level.LevelTwo and the Scene Manager will LoadScene(1). Once scene 1 (i.e. level 2) is loaded, however, the game state reverts back to Level.LevelOne. How do I go about and fix this issue.

P.S. currentLevel = Level.LevelOne is called at the Start function. Would this be part of the issue? Is the start function everytime each scene is loaded or reloaded? Thanks.

,

When a new scene is loaded, all objects in the previous scene are destroyed, and all objects in the new scene are instantiated. In order to keep values from one scene to the next, there are a few different options.

1: Use additive scene loading. With this, the old scene isn’t destroyed, but the new scene is added and loaded so both scenes can exist at the same time. You have to specially design your game around this concept though because you can’t use drag-and-drop referencing for anything except objects/behaviours within the same scene. If you had a “player avatar” in 2 scenes, then there would be a duplicate when adding the second scene additively. I don’t recommend trying this until you have a very good grasp of Unity and programming in general.

2: Use DontDestroyOnLoad(GameObject). This will make the GameObject survive changing scenes- this occurs by automatically (when the scene change is occurring) putting that GameObject into a special additive scene that is never unloaded. Similarly to additive loading, an object you set to DontDestroyOnLoad will be duplicated if you load a second scene that also contains that object- you have to be careful to either immediately destroy duplicates, or don’t have the objects in multiple scenes to begin with. The latter case can make testing scenes in the editor complicated.

3: Use static fields. It’s the object instances that are destroyed- static fields will survive just fine because they aren’t tied to any object instances to begin with. You do have to be careful about your Awake and Start functions overwriting these values though, when the new instances are made for the next scene.

4: Use static classes. Static classes, like static fields, aren’t tied to a specific object instance. You cannot make static MonoBehaviours (MonoBehaviours are by definition objects), but that’s not any huge loss. You can assign the value to a field on a static class before changing scenes, and the retrieve the value from the same place when loading a new one.

5: Save the data to a file. PlayerPrefs are an easy solution for data that persists between scenes, but fair warning that it’ll also persist between game sessions (quitting and restarting). This applies to other forms of game data saving as well- you can serialize an object and then write it to the local disk to load later. If you only want it to persist between scenes, just store the data right before the change, load it in the new scene, then destroy it (delete the key, in the case of PlayerPrefs).

I can’t really tell you which method to use as I don’t know the specific needs of your project, but some combination of these should help you with your data persistence.