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

Resolved Pause menu not working properly

Discussion in 'Scripting' started by GyurkaMolnar, Apr 25, 2024.

  1. GyurkaMolnar

    GyurkaMolnar

    Joined:
    Jan 25, 2024
    Posts:
    2
    I have this game manager script, for all kinds of stuff. I have 5 levels in my game, and the pause menu doesent work properly on the last one, even though level 5 is a duplicated version of level 1, with only a few objects added. Heres my script. The pause menu should be inactive and uninteractable in the beginning, however when i start the last level, the menu is not interactable, but is visible. It disappears only if I hit Esc to actually open the menu and press it again or click the resume button. How do i fix this? Its especially frustrating since its the last thing I need to finish.
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.SceneManagement;
    3. using UnityEngine.UI;
    4.  
    5. public class GameManager : MonoBehaviour
    6. {
    7.     public Transform player;
    8.     public int hp = 5;
    9.     public GameObject HealthUI;
    10.  
    11.  
    12.     bool End = false;
    13.     public float restartdelay=1f;
    14.     public GameObject WinUI;
    15.     public GameObject Pause;
    16.     public float speed;
    17.  
    18.     public Slider slider;
    19.     void Start()
    20.     {
    21.         Cursor.lockState = CursorLockMode.Locked;
    22.         Cursor.visible = false;
    23.         WinUI.SetActive(false);
    24.         Pause.SetActive(false);
    25.     }
    26.     private bool isPaused = false;
    27.  
    28.     void Update()
    29.     {
    30.         if (Input.GetKeyDown(KeyCode.Escape))
    31.         {
    32.             if (isPaused) ResumeGame();
    33.             else PauseGame();
    34.         }
    35.         slider.value = hp;
    36.     }
    37.  
    38.     public void PauseGame()
    39.     {
    40.         Cursor.lockState = CursorLockMode.None;
    41.         Cursor.visible = true;
    42.         Pause.SetActive(true);
    43.         Time.timeScale = 0f;
    44.         isPaused = true;
    45.     }
    46.  
    47.     public void ResumeGame()
    48.     {
    49.         Cursor.lockState = CursorLockMode.Locked;
    50.         Cursor.visible = false;
    51.         Pause.SetActive(false);
    52.         Time.timeScale = 1f;
    53.         isPaused = false;
    54.     }
    55.  
    56.     public void Win()
    57.     {
    58.         Debug.Log("Won");
    59.         WinUI.SetActive(true);
    60.         HealthUI.SetActive(false);
    61.     }
    62.    
    63.     public void EndGame()
    64.     {
    65.             if (End==false)
    66.             {
    67.             End=true;
    68.             Debug.Log("gameover");
    69.             Invoke(nameof(Restart), restartdelay);
    70.             }
    71.     }
    72.  
    73.  
    74.     public void Restart()
    75.     { SceneManager.LoadScene(SceneManager.GetActiveScene().name); }
    76. }
    77.  
     
  2. ArachnidAnimal

    ArachnidAnimal

    Joined:
    Mar 3, 2015
    Posts:
    1,936
    Add some Debug.Log to the code to know where the menu is being activated.
    Also, OnEnable() and OnDisable() are useful for debugging.
    Add a script to the menu gameObjects with a Debug.Log in OnEnable() and OnDisable() functions.
     
    Ryiah likes this.
  3. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,698
    Since the code doesn't seem to have any logic errors the most likely candidate is that it's somehow surviving into the load of level 5 and because of that
    Start
    isn't being executed and the state isn't initializing.

    To be honest this always makes me cringe. It shouldn't be responsible for "all kinds of stuff" unless you just hate yourself because when it breaks everything breaks. I also cringe whenever I see a "manager" class that isn't an actual singleton trying to keep itself unique but is responsible for unique activities like state tracking.
     
    Last edited: Apr 26, 2024
    spiney199 and Spy-Master like this.
  4. GyurkaMolnar

    GyurkaMolnar

    Joined:
    Jan 25, 2024
    Posts:
    2
    Ryiah, thanks for the help, the start() did not in fact run, caused by a stupid mistake i made when creating the level. Also I'm kind of new to all this, be kind to other newbies in the future please. A normal person would probably just say F*** the community and leave. I mean come on. Sorry for the cringe experienced caused by my naming skills :p