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. Dismiss Notice

Resolved I want to make a pause menu using Escape key as an input

Discussion in 'Input System' started by enesbagci2332, Jul 12, 2023.

  1. enesbagci2332

    enesbagci2332

    Joined:
    Apr 4, 2021
    Posts:
    82
    The problem is that I want to take the escape key's input for pausing and continuing the game but two functions happen at the same time.

    I thought about using waitforsecond method but I thought it doesn't fit at this part.
    Can you developers help me fix this problem?

    (I am trying to do a unity learn part, I'm sorry for my bad English.) (I don't want any answer, I want help like you can try using this "____" method. So I can research that method myself.)

    Code (CSharp):
    1. public void PauseGame()
    2.     {
    3.         pauseMenu.gameObject.SetActive(true);
    4.         Time.timeScale = 0;
    5.         isGamePaused = true;
    6.    
    7.     }
    8.     public void ContinueGame()
    9.     {
    10.         pauseMenu.gameObject.SetActive(false);
    11.         Time.timeScale = 1;
    12.         isGamePaused = false;
    13.     }
    14.     public void Update()
    15.     {
    16.         if (Input.GetKeyDown(KeyCode.Escape) && isGameActive)
    17.         {
    18.             PauseGame();
    19.             Debug.Log("Game should be paused rn");
    20.         }
    21.    
    22.    
    23.         if (Input.GetKeyDown(KeyCode.Escape) && isGamePaused && isGameActive)
    24.         {
    25.             ContinueGame();
    26.             Debug.Log("Game should NOT be paused rn");
    27.         }
    28.    
    29.     }
     
    Last edited: Jul 12, 2023
  2. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,900
    Try something like this:
    Code (CSharp):
    1. public void Update()
    2. {
    3.     if (Input.GetKeyDown(KeyCode.Escape))
    4.     {
    5.         if (isGamePaused)
    6.         {
    7.             ContinueGame();
    8.             Debug.Log("Game should NOT be paused rn");
    9.         } else
    10.         {
    11.             PauseGame();
    12.             Debug.Log("Game should be paused rn");
    13.         }
    14.     }
    15. }
    And always use CODE tags when you insert code in your forum posts. More info here.
     
  3. enesbagci2332

    enesbagci2332

    Joined:
    Apr 4, 2021
    Posts:
    82

    THANK YOU! I literally saw my mistake with using two if's rather than if and an else. Thanks again!

    And I saw my mistake about Code tags thanks for telling me about it! Have a good day!