Issue where Escape must be pressed 3 times to pause after Start Game

Hi guys,

This problem has persisted for some time and I’m not sure how to address it. I’ve searched extensively through Google and these forums, but nothing quite addresses the issue I’m having.

For context, I used a Brackeys tutorial on YouTube to set up a pause screen, and the pause screen works fine for the most part. The only issue I had occurred after the player returned to main menu, and then re-entered the game. The game would restart as if the pause flag was still in place, and would need to press Escape as if to ‘unpause’.

I fixed this issue with this code

    void Start()
    {
       pauseMenu.SetActive(false);
       Time.timeScale = 1f;
    }

The game can now be played on restart without issue, but it now makes it so that any time the game is started the Escape key must be pressed 3 times before pause works. After that, the game pauses without issue.

This is the rest of my code.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class PauseMenu : MonoBehaviour

{
    public GameObject pauseMenu;
    public static bool isPaused;

    // Start is called before the first frame update
    void Start()
    {
       pauseMenu.SetActive(false);
       Time.timeScale = 1f;
    }

    // Update is called once per frame
    void Update()
    {
        if(Input.GetKeyDown(KeyCode.Escape))
        {
            if(isPaused)
            {
                ResumeGame();
            }
            else
            {
                PauseGame();
            }
        }
    }

    public void PauseGame()
    {
        pauseMenu.SetActive(true);
        Time.timeScale = 0f;
        isPaused = true;
    }

    public void ResumeGame()
    {
        Time.timeScale = 1f;
        pauseMenu.SetActive(false);
        isPaused = false;
    }

    public void MainMenu()
    {
        Time.timeScale = 1f;
        SceneManager.LoadScene(0);
    }

     public void Quit()
    {
        Application.Quit();
    }
}

Any direction or suggestions would be appreciated, cheers.

1 Like

remove static from ispaused, make it only

  • public bool isPaused;

remember that you have to also set it to false in your start()

and also check your inspector that your ispaused doesnt start as true

Hi, thanks for the reply. Unfortunately, this not solve the issue. After following your instructions, nothing changed at all.

In case I misrepresented the issue, it only occurs after you first start the game, so when you enter in to the game from the main menu after launching. Once you have pressed Escape three times, the game pauses and unpauses without incident, even after returning to main menu.

add debug to your code like this

    if(Input.GetKeyDown(KeyCode.Escape))
        {
            if(isPaused)
            {
debug.log("resuming game");
                ResumeGame();
            }
            else
            {
              
debug.log("pausing game");

PauseGame();
            }
        }

and press esc carefully one by one and post the results of your console.

Check your editor value of variable, not in your IDE. I think you have different public value in editor then it your code.If value is public then Unity use value from editor and not from your code.

8938107--1225857--forum.png

1 Like

Escape/Ctrl/Alt might not work correctly in the editor. Are you trying in a build?

Well, that’s the issue. You didn’t actually set the “isPaused” flag accordingly to your manual change you did in the Start method. Why don’t you just call ResumeGame() in Start? It will disable the pause menu, will set the timescale to 1 and also makes sure isPaused is set to false. That’s why we have methods in the first place, so we don’t repeat the same code over and over again.

You also should think about where and when your “MainMenu” method will or should be used. Since it’s public it could be used from anywhere. Since you have your isPause state variable, have you actually thought about what happens when MainMenu is called when the game is paused / not paused and what that means for the actual pause state?

You essenially have a lot redundancy when it comes to the pause state and redundancy means you have to keep it all in sync. You have

  • The isPaused field
  • The pauseMenu active state
  • The timeScale state.

The change of the pause state is already wrapped in the two methods PauseGame and ResumeGame. Though It’s still up to you to keep track when you change / should change this state. When you go to the main menu you only change the timeScale but you don’t touch the pauseMenu or the isPaused state. So you’re introducing inconsistency.

You could even get rid of the isPaused field and just use the activeSelf field of the pauseMenu. Though as I said what’s more important is to think about when to actually change the state.

1 Like