Pause scene?!?
Hey there, I have a little big question.
I am a big noob so I need some help with it.
In my first scene I have the main menu. When I press the Start Button, it destroys this scene and loads an other scene, the ingame scene.
So now I want the following things to happen: - When I pres ESC the ingame scene should be paused (maybe with "Time.timeScale = 0;"). - The main menu scene should be loaded. The ingame Scene should not be destroyed. - When I press Escape again the main menu scene should be destroyed, it should go back to the ingame scene and the Game should be unpaused ("Time.timeScale = 1;").
Right now, this is my (uncomplete) code: using UnityEngine; using System.Collections; using UnityEngine.SceneManagement;
public class ButtonManagerIngame : MonoBehaviour {
bool Pause = false;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
public void PausiereSpiel(string PauseGameLevel, string newGameLevel)
{
if (Input.GetKeyDown ("escape")) {
if(Pause == true){
Time.timeScale = 1.0f;
SceneManager.LoadScene(newGameLevel);
Pause = false;
} else {
Time.timeScale = 0.0f;
Pause = true;
DontDestroyOnLoad(transform.PauseGameLevel);
}
}
}
}
Anyway I get compiler errors, but I don't think it works this way... Can anyone help me?
The Reason why I want a scene and not a canvas is, that I am not so good in Unity and I already have a finished Main menu scene I want to use for now.
Answer by FirePlantGames · Oct 14, 2016 at 08:32 PM
You can just use SceneManager.LoadScene (newGameLevel, LoadSceneMode.Additive); This adds the scene to the current scene. Using your code it would go something like this; public GameObject PauseSceneRoot;
void Start ()
{
SceneManager.LoadScene ("PauseSceneName", LoadSceneMode.Additive);
PauseSceneRoot = GameObject.FindWithTag ("PauseMenu") //Have all your pause scene objects, inside one object with the tag "PauseMenu" or this won't work
}
void Update ()
{
if (Input.GetKeyDown ("escape"))
Pause = !Pause;
Time.timeScale = (Paused)? 1.00f : 0.00f; //If your time scale isn't working, change Paused to !Paused
PauseSceneRoot.SetActive(Paused);
}
Let me know how that works.