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

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:

  1. Delete the Script on your UI Button

  2. Create this method in the main script:

    public void TogglePause()
    {
    isPaused = !isPaused;
    pauseMenu.SetActive(isPaused);
    Time.timeScale = isPaused ? 0 : 1;
    }

  3. Add this line to the “Start” method of the main script (to be sure^^)

    isPaused = true;
    TogglePause();

  4. Select the UI-Button in the GameObject-List of your current scene and open the Inspector tab

  5. Scroll down to the Button-Script and click the little plus-icon on the “OnClick()”-event panel (at the bottom)

  6. 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.

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

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