zombie2870
zombie2870

Reputation: 11

Not understanding why it is not recognising a key input

When I press escape, the pause menu in my game should become visible to the user and the game time should freeze. However, the program seems to not recognize the input when I press escape. I have tried using different Keys and they did not work either. I went to make sure that it was the input that was the problem by doing a Debug.Log command and when I tested I was still not getting any signs of it triggering. Here is the code. I hope someone can help me out.

public static bool GameIsPaused = true;

public GameObject PauseMenuUI;
// Update is called once per frame
void Update()
{
    if (Input.GetKeyDown(KeyCode.Escape))
    {
        if (GameIsPaused)
        {
            Resume();
        }
        else
        {
            Pause();
        }
    }
}

void Resume()
{
    PauseMenuUI.SetActive(false);
    Time.timeScale = 1f;
    GameIsPaused = false;
}

void Pause()
{
    PauseMenuUI.SetActive(true);
    Time.timeScale = 0f;
    GameIsPaused = true; 
}

Upvotes: 0

Views: 61

Answers (1)

Fredrik Widerberg
Fredrik Widerberg

Reputation: 3108

As we discussed in the comments, the GameObject which the PauseMenu-script is attached to was not active.

An inactive gameobject will have all its components disabled.

Update is called every frame, if the MonoBehaviour is enabled. https://docs.unity3d.com/ScriptReference/MonoBehaviour.Update.html

So your update code did not run, hence never detecting when Escape was pressed.

Upvotes: 1

Related Questions