Pause menu not working properly

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.

using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class GameManager : MonoBehaviour
{
    public Transform player;
    public int hp = 5;
    public GameObject HealthUI;


    bool End = false;
    public float restartdelay=1f;
    public GameObject WinUI;
    public GameObject Pause;
    public float speed;

    public Slider slider;
    void Start()
    {
        Cursor.lockState = CursorLockMode.Locked;
        Cursor.visible = false;
        WinUI.SetActive(false);
        Pause.SetActive(false);
    }
    private bool isPaused = false;

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Escape))
        {
            if (isPaused) ResumeGame();
            else PauseGame();
        }
        slider.value = hp;
    }

    public void PauseGame()
    {
        Cursor.lockState = CursorLockMode.None;
        Cursor.visible = true;
        Pause.SetActive(true);
        Time.timeScale = 0f;
        isPaused = true;
    }

    public void ResumeGame()
    {
        Cursor.lockState = CursorLockMode.Locked;
        Cursor.visible = false;
        Pause.SetActive(false);
        Time.timeScale = 1f;
        isPaused = false;
    }

    public void Win()
    {
        Debug.Log("Won");
        WinUI.SetActive(true);
        HealthUI.SetActive(false);
    }
   
    public void EndGame()
    {
            if (End==false)
            {
            End=true;
            Debug.Log("gameover");
            Invoke(nameof(Restart), restartdelay);
            }
    }


    public void Restart()
    { SceneManager.LoadScene(SceneManager.GetActiveScene().name); }
}

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.

1 Like

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.

2 Likes

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 fuck the community and leave. I mean come on. Sorry for the cringe experienced caused by my naming skills :stuck_out_tongue: