Search Unity

  1. Unity 2019.1 is now released.
    Dismiss Notice

Pause menu

Discussion in 'Scripting' started by Laurenz_02, May 23, 2019.

  1. Laurenz_02

    Laurenz_02

    Joined:
    Oct 21, 2017
    Posts:
    52
    Hey!

    I currently made a pause menu for my game. It is just a simple scene with the text: "Pause game" and there is a button: "Resume game".

    In every level I have a pause button. When I click that button, I set the timeScale equal to zero, so that everything stops and I load my pause scene with SceneManager.LoadScene...

    As I have 8 different scenes, how can I make it so that the "Resume game" button loads the correct scene?

    Example: I am playing level 4, and I need to go to the toilet. I pause the game. When I come back I click the resume game button, and my game resumes level 4. A moment later I reach level 7 and I need to do something else, I pause the game, and when I come back and click the resume game button, my game needs to load level 7.

    How can I do this?

    Thanks in advance! :)
     
  2. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    1,674
    Switching scenes can be fairly complex, because it means that you (the developer) will need to preserve the state of everything currently running your scene, and restore it when you resume from Pause. That's not necessarily an easy task.

    Two other approaches I'd use before your scene-switching approach:
    • Make your Pause menu a prefab that you instantiate in the same scene. I don't know how complex your pause menu is, and whether it really needs to be its own scene. But my pause menu is just a canvas which can easily sit on top of the paused game.
    • Consider additively loading your scene rather than switching to it. You can have two (or more) scenes loaded at once, so it seems easier to additively load your pause scene, then destroy it when resuming. That way your real scene doesn't lose its state.
     
    Laurenz_02 likes this.