#because you reload the scene, those
1 messages · Page 1 of 1 (latest)
no
Let me find out
learn how to get a reference
or don't make your game manager DDOL and instead just include it in the scenes you need it in
Wait, i found it
using UnityEngine;
using UnityEngine.SceneManagement;
using TMPro;
public class GameManager : MonoBehaviour
{
public static GameManager instance;
public GameObject gameOverUI;
public TextMeshProUGUI restartCountdownText;
private PlayerShooting playerShot;
private FPSController fpsController;
private PauseController pause;
private float initialCountdownTime = 10f;
private float currentCountdownTime;
private void Awake()
{
if (instance == null)
{
instance = this;
DontDestroyOnLoad(gameObject);
}
else if (instance != this)
{
Destroy(gameObject);
}
}
private void OnEnable()
{
SceneManager.sceneLoaded += OnSceneLoaded;
}
private void OnDisable()
{
SceneManager.sceneLoaded -= OnSceneLoaded;
}
private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
{
playerShot = FindObjectOfType<PlayerShooting>();
fpsController = FindObjectOfType<FPSController>();
pause = FindObjectOfType<PauseController>();
}
private void Start()
{
playerShot = FindObjectOfType<PlayerShooting>();
fpsController = FindObjectOfType<FPSController>();
pause = FindObjectOfType<PauseController>();
}
public void GameOver()
{
gameOverUI.SetActive(true);
Time.timeScale = 0;
playerShot.enabled = false;
Cursor.visible = true;
pause.enabled = false;
if (fpsController != null)
{
fpsController.enabled = false;
}
currentCountdownTime = initialCountdownTime;
StartCoroutine(RestartCountdown());
}
private IEnumerator RestartCountdown()
{
while (currentCountdownTime > 0)
{
restartCountdownText.text = Mathf.CeilToInt(currentCountdownTime).ToString();
yield return null;
currentCountdownTime -= Time.deltaTime;
if (Input.GetKeyDown(KeyCode.Space))
{
currentCountdownTime -= 1;
}
}
RestartGame();
}
public void RestartGame()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
Time.timeScale = 1;
playerShot.enabled = true;
Cursor.visible = false;
pause.enabled = true;
if (fpsController != null)
{
fpsController.enabled = true;
}
restartCountdownText.text = "";
}
}
Do it should to be like this?
@turbid notch Like this?