- Home /
Making a pause menu resume
So I'm trying to make a pause feature in my game, and that works perfectly. Unfortunately, the game stays paused infinitely, which isn't so perfect. The way I have it programmed seems like it should work, at least to me, but it doesn't. Any help? Here's the code:
void FixedUpdate()
{
if (Input.GetKeyDown("escape"))
{
paused = !paused;
}
if (paused == true)
{
Time.timeScale = 0;
}
if (paused == false)
{
Time.timeScale = 1;
}
You should only use FixedUpdate for physics. It can come back to haunt you if you don't. Use coroutines, or deltatime in update, for anything time based.
Also, line 13 should just read "else". A boolean can only be two things, so checking if it's the other is completely unnecessary and slows down the game (although admittedly it would take a ton of if statements to be noticeable).
Answer by flashframe · May 16, 2016 at 04:28 PM
It's because you're using FixedUpdate which isn't called when timeScale is set to zero. http://docs.unity3d.com/ScriptReference/Time-timeScale.html
Try changing it to Update(). That should work.
How would you make it so you could have a UI button ins$$anonymous$$d of pressing escape? Is there a OnButtonPress function or something like it?
@aballif If you are using a Unity UI Button component, then you can set an "OnClick" Event in the inspector. You can drag your custom script in there and assign the method you want to run.
Answer by vittu1994 · May 16, 2016 at 04:21 PM
can you toggle between each boolean with that first if-statement? In that case you should write the two others like this:
if(paused)
{
Time.timeScale = 0;
}
else if(!paused)
{
Time.timeScale = 1;
}
paused is paused = true and !paused = paused = false.
Your answer
Follow this Question
Related Questions
Game doesn't get paused in Release build 0 Answers
Pausing an Invoke for a time 2 Answers
Question on timeScale and boolean script. 0 Answers
Unpause game when the timer reaches 0 1 Answer
Confusion on simple pause screen 1 Answer