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. : )