Not getting keyboard input while "paused"

Greetings,

I’m trying to add a pause screen to my 3D space shooter. I followed a tutorial which said to use Time.timeScale = 0 to accomplish this.

I created a PauseController script and put it on an object in my game. It correctly pauses the game and the music but I am unable to get keyboard input to unpause the game in that state. I even added a coroutine to check for the keyboard input but my ResumeGame() function is never getting hit.

Here’s the code for the script. How can I fix this?

using System.Collections;
using UnityEngine;

public class PauseManager : MonoBehaviour
{
    public static bool gamePaused = false;

    void FixedUpdate()
    {
        if (Input.GetKeyDown(KeyCode.F9))
        {
            TogglePauseState();
        }
    }

    public void PauseGame()
    {
        Debug.Log("Pausing game");
        AudioListener.pause = true;
        Time.timeScale = 0;
        gamePaused = true;
        StartCoroutine(CheckForUnpause());
    }

    public void ResumeGame()
    {
        Debug.Log("Resuming game");
        AudioListener.pause = false;
        Time.timeScale = 1;
        gamePaused = false;
        StopCoroutine(CheckForUnpause());
    }

    public void TogglePauseState()
    {
        if (gamePaused)
        {
            ResumeGame();
        }
        else
        {
            PauseGame();
        }
    }

    IEnumerator CheckForUnpause()
    {
        while (gamePaused)
        {
            yield return new WaitForSeconds(0.1f);
            if (Input.GetKeyDown(KeyCode.F9))
            {
                ResumeGame();
            }
        }
    }

}

Thanks,

Greg.

FixedUpdate is not called when timeScale is set to zero. Why would you do all of this in FixedUpdate in the first place? Only put actual physics there. Use Update for everything else, and LateUpdate for visuals that need to happen after the scene was updated.

That was it. I initially used Update and it didn’t work but it turns out it was because I was using the P key which the unity editor uses for Pause and they were conflicting. I changed to FixedUpdate while also switching to use F9. That will teach me not to change more than one thing at a time when troubleshooting :slight_smile:

That’s a good tradeoff lesson to take away from being stuck on this kind of problem, agreed :smile:
Glad it works now.

I’m actually having another issue with this. I’m adding some UI components (Game Paused text and a button to resume the game). The text and button are rendering but the button click doesn’t seem to be firing:
6131642--668606--upload_2020-7-25_16-18-3.png

6131642--668609--upload_2020-7-25_16-18-27.png

6131642--668612--upload_2020-7-25_16-18-34.png

public class PauseManager : MonoBehaviour
{
    [SerializeField]
    private GameObject pauseScreen = null;
    public static bool gamePaused = false;
    private bool _showGui = false;

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.F9))
        {
            TogglePauseState();
        }
    }

    public void PauseGame(bool showGui=true)
    {
        _showGui = showGui;
        Debug.Log("Pausing game");
        if (pauseScreen != null && showGui)
        {
            pauseScreen.SetActive(true);
        }
        AudioListener.pause = true;
        Time.timeScale = 0;
        gamePaused = true;
    }

    public void ResumeGame()
    {
        Debug.Log("Resuming game");
        if (pauseScreen != null && _showGui)
        {
            pauseScreen.SetActive(false);
        }
        AudioListener.pause = false;
        Time.timeScale = 1;
        gamePaused = false;
    }

    public void TogglePauseState()
    {
        if (gamePaused)
        {
            ResumeGame();
        }
        else
        {
            PauseGame();
        }
    }

}

Apparently the TextMeshPro button click handler doesn’t work when timeScale is 0. I tried a regular button and it worked. Pity.