Created
October 27, 2023 20:37
-
-
Save TheMehranKhan/ec73db3ead2ccf26e4bcd6043194cc17 to your computer and use it in GitHub Desktop.
A game over manager that is responsible for showing the game over UI and handling game restart.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Author: themehrankhan | |
using UnityEngine; | |
using UnityEngine.SceneManagement; | |
/// <summary> | |
/// A game over manager that is responsible for showing the game over UI and handling game restart. | |
/// | |
/// This manager can be used to centralize the game's game over logic, making it easier to maintain and update. | |
/// | |
/// To use the game over manager, simply attach this script to a game object in your scene. | |
/// You can then access the game over manager instance from anywhere in your code using the | |
/// `GameOverManager.Instance` property. | |
/// | |
/// To show the game over UI and pause the game, call the `GameOver()` method. | |
/// | |
/// To restart the game, call the `RestartGame()` method. | |
/// </summary> | |
public class GameOverManager : MonoBehaviour | |
{ | |
/// <summary> | |
/// The singleton instance of the game over manager. | |
/// </summary> | |
public static GameOverManager Instance { get; private set; } | |
/// <summary> | |
/// The game over UI object. | |
/// </summary> | |
public GameObject gameOverUI; | |
private void Awake() | |
{ | |
// If there is already a game over manager instance, destroy this instance. | |
if (Instance != null) | |
{ | |
Destroy(this); | |
return; | |
} | |
// Set the singleton instance of the game over manager. | |
Instance = this; | |
} | |
/// <summary> | |
/// Shows the game over UI and pauses the game. | |
/// </summary> | |
public void GameOver() | |
{ | |
// Show game over UI | |
gameOverUI.SetActive(true); | |
// Pause the game | |
Time.timeScale = 0f; | |
} | |
/// <summary> | |
/// Reloads the current scene and restarts the game. | |
/// </summary> | |
public void RestartGame() | |
{ | |
// Reload the current scene | |
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment