Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

Question Closing the pause-menu.

Discussion in 'Accessibility' started by mr_pineapple_44, Dec 12, 2023.

  1. mr_pineapple_44

    mr_pineapple_44

    Joined:
    Aug 18, 2023
    Posts:
    5
    Hi! I want to make the resume-button to close the pausemenu. How do I do fix it?

    This is what the pausemenu looks like:

    upload_2023-12-12_23-23-41.png

    Here are my scripts:

    PauseHandlerScript:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PauseHandlerScript : MonoBehaviour
    6. {
    7.     // Start is called before the first frame update
    8.  
    9.     public bool paused = false;
    10.  
    11.     public GameObject pauseMenu;
    12.  
    13.     void Start()
    14.     {
    15.         pauseMenu.GetComponent<PauseMenuScript>();
    16.     }
    17.  
    18.     // Update is called once per frame
    19.     void Update()
    20.     {
    21.  
    22.         if (Input.GetKeyDown(KeyCode.Tab))
    23.         {
    24.             paused = !paused;
    25.         }
    26.         if (paused)
    27.         {
    28.             Time.timeScale = 0;
    29.             pauseMenu.SetActive(paused);
    30.             AudioListener.pause = true;
    31.  
    32.         }
    33.         else
    34.         {
    35.             Time.timeScale = 1;
    36.             pauseMenu.SetActive(paused);
    37.             AudioListener.pause = false;
    38.         }
    39.     }
    40.  
    41.     public void ClosePauseMenu()
    42.     {
    43.         pauseMenu.SetActive(false);
    44.         Time.timeScale = 0;
    45.     }
    46. }
    47.  
    PauseMenuScript:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using TMPro;
    4. using UnityEngine;
    5. using UnityEngine.UI;
    6. using UnityEngine.SceneManagement;
    7.  
    8. public class PauseMenuScript : MonoBehaviour
    9. {
    10.     [SerializeField] GameObject pauseMenu;
    11.     public TextMeshProUGUI titleText;
    12.     public Button resumeButton;
    13.  
    14.     public bool pauseBool = false;
    15.  
    16.     // Start is called before the first frame update
    17.     void Start()
    18.     {
    19.         gameObject.SetActive(pauseBool);
    20.  
    21.     }
    22.  
    23.     // Update is called once per frame
    24.     void Update()
    25.     {
    26.  
    27.     }
    28.  
    29.  
    30.     public void ActivePauseMenu()
    31.     {
    32.         pauseMenu.SetActive(true);
    33.     }
    34.  
    35.     public void ClosePauseMenu()
    36.     {
    37.         pauseMenu.SetActive(false);
    38.     }
    39.  
    40. }
    41.  
    When I added the canvas on the OnClick-function the resume-button does not work when I select Function, PauseHandlerScript and ClosePauseMenu () to close down the pausemenu.
     

    Attached Files:

  2. JuliaP_Unity

    JuliaP_Unity

    Unity Technologies

    Joined:
    Mar 26, 2020
    Posts:
    709
    Hello, your question is not really accessibility related, it's general programming.

    The answer though is that if you want to unpause/resume through the button, you need to make sure that when you click it, exactly the same logic you used on your Update function also runs: set the correct value for the paused variable, set the time scale to 1 (not zero, since you're unpausing/resuming), and don't forget to reactivate your AudioListener!

    Highly recommend separating this logic in its own method so you can always run the same logic, either in the Update because of the key press, or in the button click logic.

    Hope this helps :)
     
  3. mr_pineapple_44

    mr_pineapple_44

    Joined:
    Aug 18, 2023
    Posts:
    5
    I wrote the time scale to 1, but I don't know how I should seperate the logic in its own method.