- Home /
Problem changing TimeScale
I'm trying to pause and unpause a small game by setting the timescale to 0 and then back to 1 to unpause using this script:
bool paused=false;
void Update ()
{
if (Input.GetKeyDown(KeyCode.Escape))
{
Debug.Log ("esc: "+paused);
if (paused)
{
Debug.Log("unpause");
Time.timeScale=1;
paused=false;
}
if(!paused)
{
Time.timeScale=0;
paused=true;
}
Debug.Log (paused + " "+Time.timeScale);
}
}
Pressing Esc twice in game prints this to the console
esc: False
True 0
esc: True
unpause
True 0
The if(paused) loop is being called but the timescale and paused lines are not being run.
Any help is appreciated.
Answer by getyour411 · Feb 01, 2014 at 08:50 PM
if(paused) set the bool = false, so the if(!paused) is now true and it executes it. Restructure this to something like
if (Input.GetKeyDown (KeyCode.Escape)) {
if (!paused) {
Time.timeScale = 0;
paused = true;
} else {
paused = false;
Time.timeScale = 1;
}
}
whoops. Figured it was something simple like this. I always forget else if.
Your answer
Follow this Question
Related Questions
Pause, Unpause 1 Answer
Pause menu... Isn't pausing everything... 1 Answer
How to Appease a pause menu in a game that changes it's time scale? 1 Answer
TimeScale = 0 crashes Unity 1 Answer
How to Pause game 1 Answer