0

I am new to unity and working on a projects. I want to work with multiple scenes.

some of my scenes are like option menu in the game.

from my main screen I want to open an options scene and when I am done I want to move back to my main scene and when I am back I want to keep the things done in main scene before the options scene opens

I can change scenes with SceneManager but it loads the screen as new as if I did nothing there is it possible to switch between loaded scenes without loading again? I think that if it is; I can continue from the progress in the main scene if it is not how can I continue my progress (do I have to keep all the data and when the scene starts load back from that data? )

2
  • I suppose you should save your progress in PlayerPrefs (for example) and load it after Commented Apr 9, 2020 at 12:42
  • 1
    scene manager has scenecount (the total number of currently loaded scenes) by that I assumed I can switch to a loaded scene instead of loadiing it again and if I can switch back the the thing I done in that scene would be there Commented Apr 9, 2020 at 12:58

2 Answers 2

1

The SceneManager class provides lots of useful ways to manage your scenes. You can find documentation here.

Using multiple scenes to separate your logic is a great approach, and using the LoadSceneMode.Additive option when loading a new scene lets you load one scene alongside another.

To achieve what you want, you'd roughly need to do the following:

  1. Load your main menu scene.
  2. Load your options scene additively with a call like SceneManager.LoadScene("path/to/options/scene.unity", LoadSceneMode.additive).
  3. Pass input control to your other scene.
  4. Unload the options scene when you are finished with it.
  5. The main menu scene will have been loaded for the entire time, and you won't have to "remember" any values or use DontDestroyOnLoad.

An alternative option is to house all of your menu functionality in a single scene, and switch between multiple canvases. You can find information about that here.

Sign up to request clarification or add additional context in comments.

4 Comments

I want to use multiple scenes If i load scenes additively new scene will appear on top of old when I tried that new scene and main scene looks mixed (maybe because I didnt give a background to that)
also when tried additively I get an error about 2 audiolistener in the scene maybe using dontDestroyOnLoad is better than working additively
@eomer In my professional experience, DontDestroyOnLoad is a powerful tool that should be used with caution, since it can introduce sneaky bugs. I'm not saying it won't work, but you'll have to be careful using it to make sure your game state is valid when you return to the main menu. Separately, the error regarding 2 AudioListeners happens because you have a camera in each scene with that component attached.
I also learned that DontDestroyOnLoad works only on root elements of the hierarchy, it can't be applied to a single child so it will be hard for me to use
0

you can create a class with values that you want to keep and put it on a gameobject and use DontDestroyOnLoad(this.gameObject); so the object won't be removed when you load a new scene

3 Comments

I looked into it and it may work but can I also use for prefabs maybe I use dontdestroyonload for the game Object I Instantiate the prefabs in
or you could just put DontDestroyOnLoad(this.gameObject) on the player it's self
player?? I dont understand.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.