Restart Scene Problem

Hi ! I made a game and wanted to make multiple levels.
At first I made a script that if the player pressed on the r key, the game would restart simply by using
SceneManager.LoadScene(“SCENENAME”);
then I made another scene(level 2), and I wanted to keep the camera light player gamemanager etc… I used DontDestroyOnLoad method, and it really kept the object when loaded the next level, the problem is, that If I want to restart the game I simply cant, it just wont work,
I tried loading the first lvl using the same method, did not work.
tried the second one, did not work, tried active scene as well.
before its loading the next lvl I can restart the game as many times as I want though.
What is the problem exactly? is it not a good idea to use the DontDestroyOnLoad method?

Hi @daniel_7866

I can’t understand exactly what is causing your problem just by reading you description, but I can put you on the right track.

DontDestroyOnLoad may be useful from time to time, but it is best to work with multi scene editing. Instead of calling DontDestroyOnLoad to keep your gameobjects alive between scenes, you can load multiple scenes at the same time, with each scene having its own responsibility.

In your case you could work with two scenes loaded every time, one would be a ManagerScene, it will contain all manager scripts, like the GameController for example, and all other gameobjects that must be alive during the whole game, like the camera and maybe some lights and player like you said. The other loaded scene would be your LevelScene, the scene that contains the scenario of your level and other stuff. This way you can always keep the ManagerScene loaded and just change the LevelScene as the player progresses in the game.

You must be aware that to have more than one scene loaded at the same time, you will have to use LoadSceneMode.Additive when calling SceneManager.LoadScene(), other wise unity will unload all loaded scenes before load the next one. You will also need to unload the LevelScene when the level is over, using SceneManager.UnloadSceneAsync(). Furthermore, you can load your scenes asynchronously, using SceneManager.LoadSceneAsync(), that way you can still have your code running while the scene is being loaded, but that is a topic for another day.

Hope I could have helped.