- Home /
Unpausing Game with GUI
I currently have this code in my OnGUI portion of a script. The game pauses just fine when I click the button the first time, but then won't unpause upon pressing the button again. What am I missing?
if(GUI.Button(Rect(300,5,305,25),btnTexture) && !paused)
{
Debug.Log("HIT1");
Time.timeScale=0;
btnTexture=playTexture;
paused=true;
}
else if(GUI.Button(Rect(300,5,305,25),btnTexture))
{
Debug.Log("HIT2");
Time.timeScale=1;
btnTexture=pauseTexture;
paused=false;
}
Changed up the ordering as suggested but didn't change anything. Hit2 still not showing up in the console. I have the script this is in attached to a sphere in-game... does that kill GUI responsiveness?
Answer by DaveA · Jan 15, 2011 at 01:45 AM
I would rearrange it a bit:
private var paused = false; ....
if(GUI.Button(Rect(300,5,305,25),btnTexture))
{
if (!paused)
{
Debug.Log("HIT1");
Time.timeScale=0;
btnTexture=playTexture;
paused=true;
}
else
{
Debug.Log("HIT2");
Time.timeScale=1;
btnTexture=pauseTexture;
paused=false;
}
}
$$anonymous$$ake sure you declare 'paused' outside of your function so it persists between calls.
Paused already declared outside the function. Thanks for the attempt though :)
Found the problem. $$anonymous$$ade a mistake when I updated the code with your fix and made part of it unreachable. Works great now!
Answer by Jessy · Jan 15, 2011 at 02:02 AM
The button is only registered as pressed once in OnGUI, so your else if can never fire. Instead, do this:
Edit: I was AFK and DaveA answered with the same code. This code looks formatted, though. ;-)
function OnGUI () {
if (GUI.Button(Rect(300,5,305,25), btnTexture) {
if (paused) {
Debug.Log("Unpaused");
btnTexture = pauseTexture;
Time.timeScale = 1;
paused = false;
}
else {
Debug.Log("Paused");
btnTexture = playTexture;
Time.timeScale = 0;
paused = true;
}
}
}
Answer by ChrisSch · Apr 29, 2013 at 06:33 PM
Thank you all of you this helped me too. ^_^ I'm making a tower defense game and needed a pause for when the build menu is open and this did the trick.
Your answer
![](https://koobas.hobune.stream/wayback/20220613095531im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Pause Button Script 4 Answers
Sound on pause it doesn't works. 2 Answers
How do I pause and unpause only some GameObjects at a time 1 Answer
Pausing and unpausing the game with one button 2 Answers
In game pause menu 2 Answers