Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Resolved Not getting keyboard input while "paused"

Discussion in 'Scripting' started by MidniteOil, Jul 25, 2020.

  1. MidniteOil

    MidniteOil

    Joined:
    Sep 25, 2019
    Posts:
    345
    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?

    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine;
    3.  
    4. public class PauseManager : MonoBehaviour
    5. {
    6.     public static bool gamePaused = false;
    7.  
    8.     void FixedUpdate()
    9.     {
    10.         if (Input.GetKeyDown(KeyCode.F9))
    11.         {
    12.             TogglePauseState();
    13.         }
    14.     }
    15.  
    16.     public void PauseGame()
    17.     {
    18.         Debug.Log("Pausing game");
    19.         AudioListener.pause = true;
    20.         Time.timeScale = 0;
    21.         gamePaused = true;
    22.         StartCoroutine(CheckForUnpause());
    23.     }
    24.  
    25.     public void ResumeGame()
    26.     {
    27.         Debug.Log("Resuming game");
    28.         AudioListener.pause = false;
    29.         Time.timeScale = 1;
    30.         gamePaused = false;
    31.         StopCoroutine(CheckForUnpause());
    32.     }
    33.  
    34.     public void TogglePauseState()
    35.     {
    36.         if (gamePaused)
    37.         {
    38.             ResumeGame();
    39.         }
    40.         else
    41.         {
    42.             PauseGame();
    43.         }
    44.     }
    45.  
    46.     IEnumerator CheckForUnpause()
    47.     {
    48.         while (gamePaused)
    49.         {
    50.             yield return new WaitForSeconds(0.1f);
    51.             if (Input.GetKeyDown(KeyCode.F9))
    52.             {
    53.                 ResumeGame();
    54.             }
    55.         }
    56.     }
    57.  
    58. }
    59.  
    Thanks,

    Greg.
     
  2. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,606
    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.
     
  3. MidniteOil

    MidniteOil

    Joined:
    Sep 25, 2019
    Posts:
    345
    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 :)
     
  4. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,606
    That's a good tradeoff lesson to take away from being stuck on this kind of problem, agreed :D
    Glad it works now.
     
  5. MidniteOil

    MidniteOil

    Joined:
    Sep 25, 2019
    Posts:
    345
    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:
    upload_2020-7-25_16-18-3.png

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

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

    Code (CSharp):
    1. public class PauseManager : MonoBehaviour
    2. {
    3.     [SerializeField]
    4.     private GameObject pauseScreen = null;
    5.     public static bool gamePaused = false;
    6.     private bool _showGui = false;
    7.  
    8.     void Update()
    9.     {
    10.         if (Input.GetKeyDown(KeyCode.F9))
    11.         {
    12.             TogglePauseState();
    13.         }
    14.     }
    15.  
    16.     public void PauseGame(bool showGui=true)
    17.     {
    18.         _showGui = showGui;
    19.         Debug.Log("Pausing game");
    20.         if (pauseScreen != null && showGui)
    21.         {
    22.             pauseScreen.SetActive(true);
    23.         }
    24.         AudioListener.pause = true;
    25.         Time.timeScale = 0;
    26.         gamePaused = true;
    27.     }
    28.  
    29.     public void ResumeGame()
    30.     {
    31.         Debug.Log("Resuming game");
    32.         if (pauseScreen != null && _showGui)
    33.         {
    34.             pauseScreen.SetActive(false);
    35.         }
    36.         AudioListener.pause = false;
    37.         Time.timeScale = 1;
    38.         gamePaused = false;
    39.     }
    40.  
    41.     public void TogglePauseState()
    42.     {
    43.         if (gamePaused)
    44.         {
    45.             ResumeGame();
    46.         }
    47.         else
    48.         {
    49.             PauseGame();
    50.         }
    51.     }
    52.  
    53. }
     
  6. MidniteOil

    MidniteOil

    Joined:
    Sep 25, 2019
    Posts:
    345
    Apparently the TextMeshPro button click handler doesn't work when timeScale is 0. I tried a regular button and it worked. Pity.