Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

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

Discussion in 'Scripting' started by FaleWhail, Apr 9, 2023.

  1. FaleWhail

    FaleWhail

    Joined:
    Sep 29, 2022
    Posts:
    4
    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

    Code (CSharp):
    1.     void Start()
    2.     {
    3.        pauseMenu.SetActive(false);
    4.        Time.timeScale = 1f;
    5.     }
    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.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class PauseMenu : MonoBehaviour
    7.  
    8. {
    9.     public GameObject pauseMenu;
    10.     public static bool isPaused;
    11.  
    12.     // Start is called before the first frame update
    13.     void Start()
    14.     {
    15.        pauseMenu.SetActive(false);
    16.        Time.timeScale = 1f;
    17.     }
    18.  
    19.     // Update is called once per frame
    20.     void Update()
    21.     {
    22.         if(Input.GetKeyDown(KeyCode.Escape))
    23.         {
    24.             if(isPaused)
    25.             {
    26.                 ResumeGame();
    27.             }
    28.             else
    29.             {
    30.                 PauseGame();
    31.             }
    32.         }
    33.     }
    34.  
    35.     public void PauseGame()
    36.     {
    37.         pauseMenu.SetActive(true);
    38.         Time.timeScale = 0f;
    39.         isPaused = true;
    40.     }
    41.  
    42.     public void ResumeGame()
    43.     {
    44.         Time.timeScale = 1f;
    45.         pauseMenu.SetActive(false);
    46.         isPaused = false;
    47.     }
    48.  
    49.     public void MainMenu()
    50.     {
    51.         Time.timeScale = 1f;
    52.         SceneManager.LoadScene(0);
    53.     }
    54.  
    55.      public void Quit()
    56.     {
    57.         Application.Quit();
    58.     }
    59. }
    Any direction or suggestions would be appreciated, cheers.
     
    JClay91 likes this.
  2. karderos

    karderos

    Joined:
    Mar 28, 2023
    Posts:
    376
    remove static from ispaused, make it only

    1. 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
     
  3. FaleWhail

    FaleWhail

    Joined:
    Sep 29, 2022
    Posts:
    4
    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.
     
  4. karderos

    karderos

    Joined:
    Mar 28, 2023
    Posts:
    376
    add debug to your code like this

    Code (CSharp):
    1.     if(Input.GetKeyDown(KeyCode.Escape))
    2.         {
    3.             if(isPaused)
    4.             {
    5. debug.log("resuming game");
    6.                 ResumeGame();
    7.             }
    8.             else
    9.             {
    10.              
    11. debug.log("pausing game");
    12.  
    13. PauseGame();
    14.             }
    15.         }
    and press esc carefully one by one and post the results of your console.
     
  5. MartinMa_

    MartinMa_

    Joined:
    Jan 3, 2021
    Posts:
    455
    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.
     

    Attached Files:

    angrypenguin likes this.
  6. Stardog

    Stardog

    Joined:
    Jun 28, 2010
    Posts:
    1,890
    Escape/Ctrl/Alt might not work correctly in the editor. Are you trying in a build?
     
  7. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,571
    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
    Code (CSharp):
    1. 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
    1. The isPaused field
    2. The pauseMenu active state
    3. 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.
     
    angrypenguin likes this.