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); }
}