- Home /
Resume Button / Pause Menu odd interaction
This has been driving me crazy for 3 hours, I hope you guys can help me. It's not easy being a decent artist but a newbie coder. Unity certainly narrows the gap a lot but at some point, one still runs into tougher issues.
I have a simple Pause Menu GameObject with 3 UI buttons that toggles ON or OFF when you press the Escape key. Works fine, no issues here. All of this happens within the Update function (with the exception of the variable definitions).
// Toggle Pause Menu ON or OFF
if (Input.GetButtonDown("Cancel"))
{
if (!isPaused)
{
isPaused = true;
pauseMenu.SetActive(true);
Time.timeScale = 0;
}
else
{
isPaused = false;
pauseMenu.SetActive(false);
Time.timeScale = 1;
}
}
One of these 3 buttons is a Resume button that basically fulfills the same role as unpausing with the Escape key after it's already paused. But when I click it, everything works fine with one exception: I have to press the Escape key twice to pause the game again. It will not recognize the first key press.
The button itself is a UI Button with a script attached that executes an On Click () function. And here's the function itself from the script:
// Resume (Go back to game screen)
public void OnResume()
{
isPaused = false;
pauseMenu.SetActive(false);
Time.timeScale = 1;
}
I know that everything within the Update function is called every frame and I know the OnResume() function is only called when I click the UI Button. So what am I missing?
I assume the OnResume() function doesn't set the isPaused boolean variable to false (even though I clearly added the line) which is why the first time I press Escape, nothing seems to happen (but the if statement in the Update function probably sets it to false) and then the second time it happens because it sets it to true.
Thank you so much for reading. I look forward to your responses. : )
Answer by kit20 · Jun 09, 2016 at 12:03 PM
@ColdJackle - You were right about the variable not being synced properly because of lack of communication between the methods. Apparently the solution was very simple: define the isPasued variable as static.
static bool isPaused = false;
That took care of everything. Don't even need to initialize anything in Start () now. : )
Answer by ColdJackle · Jun 07, 2016 at 11:38 AM
Is the "isPaused" variable in the button script the same as the one in the menu script? Because it sounds like you have 2 scripts and 2 variables, but you didn't provide the part when theses two variables are synced. The Menu-Script has to be notified about the change. I would recommend the following:
Delete the Script on your UI Button
Create this method in the main script:
public void TogglePause() { isPaused = !isPaused; pauseMenu.SetActive(isPaused); Time.timeScale = isPaused ? 0 : 1; }
Add this line to the "Start" method of the main script (to be sure^^)
isPaused = true; TogglePause();
Select the UI-Button in the GameObject-List of your current scene and open the Inspector tab
Scroll down to the Button-Script and click the little plus-icon on the "OnClick()"-event panel (at the bottom)
Drag the Gameobject with the menu-script on the empty object field in the new event item and select the "TogglePause"-method from the class->method list.
EDIT: Oh and Replace all the code you wrote in that if-clause with a call to the TogglePause()-method, like this:
if (Input.GetButtonDown("cancel")) { TogglePause(); }
This is like calling the method from OnClick, but with less coding. Also you can call functions with one primitve parameter (int, float, bool, etc.), that you can set, or an UI-Object, like the clicked button.
Hope this helps :)
@ColdJackle - Thank you very much for your reply. But now in addition to having to press the Escape key twice to get the pause menu to come up again... I also have to click Resume twice to get back to the game. I recreated this specific section in a brand new scene but I have the same issue. This happens both in the Editor and in a Standalone build. Here's the full script:
using UnityEngine;
using System.Collections;
public class $$anonymous$$ainScript : $$anonymous$$onoBehaviour
{
private bool isPaused = false;
public GameObject pause$$anonymous$$enu;
// Use this for initialization
void Start ()
{
isPaused = false;
}
// Update is called once per frame
void Update ()
{
if (Input.GetButtonDown("Cancel"))
{
TogglePause();
}
}
public void TogglePause()
{
isPaused = !isPaused;
pause$$anonymous$$enu.SetActive(isPaused);
Time.timeScale = isPaused ? 0 : 1;
}
public void OnQuit()
{
#if UNITY_STANDALONE
Application.Quit();
#endif
#if UNITY_EDITOR
UnityEditor.EditorApplication.isPlaying = false;
#endif
}
}
It's attached to the Canvas since I'm never gonna destroy or disable that object as well as the Resume and Quit buttons, each calling its specific method when clicked.
That's all there is in the scene but if you want to take a direct look at it, I can pack it into a zip file and upload it to my Dropbox.
Ah, I see a wrong step in my reply: Please switch "isPaused = false" in "Start()" to "isPaused = true; TogglePause();". This way the everthing gets set fine. If you still encounter any double presses, then there is something wrong with Unity, because I recreated the scene and except for the missing code in Start, everything is working for me.
I'm running the Unity version 5.3.5f1 Personal Edition, if that helps.
Sadly, I still can't get it to work flawlessly. Now I have to press the Escape key 5 times before the menu comes up the first time. Then double presses to toggle it on and off again as well as double press to Resume. Which makes no sense at all to me.
But thanks for taking an interest in my issue, I really appreciate any bit of help I can get.
Your answer
Follow this Question
Related Questions
I made an resume button, but I don't know how to code it to close my pause menu, any tips? 2 Answers
Why doesnt my resume button work 0 Answers
Buttons will not change color 11 Answers
Is it a bad idea to add and remove listeners for toggles and buttons at runtime? 2 Answers
GUI window help 1 Answer