The GameOverManager
handles all end-of-level UI states and logic. It controls the display of the game over screen, updates the final message based on win or loss conditions, and provides functionality for restarting the level, moving to the next level, or returning to the main menu. This script is UI-driven and ties directly into the scene management system in Unity.
It serves as the final controller for player feedback and game flow post-completion.
GameOverManager
Namespace: global
Inherits from: MonoBehaviour
GameObject gameOverUI
Reference to the UI canvas that contains all game over elements. It is initially hidden at runtime.
TMP_Text gameOverText
Text element that displays a contextual win/loss message to the player.
GameObject nextLevelButton
Button for progressing to the next level, shown only on victory.
GameObject resetLevelButton
Button for restarting the current level, shown only on failure.
bool isGameOver = false
Global flag indicating whether the game is in a game-over state. Used by other systems to disable input or interaction when the round ends.
void Awake()
Searches the scene (including inactive objects) to find and assign critical UI elements, including the game over canvas, text, and control buttons.
FindObjectsOfType<T>(true)
to ensure inactive UI objects can be located.GameOver_Canvas
→ game_over bg
→ children.
void ShowGameOver(bool isWin)
Activates the game over UI and updates messaging and controls based on the outcome.
isWin
: true
to show the victory message and enable the next level button, or false
to show failure message and enable restart.Time.timeScale = 0f
).
void NextLevel()
Loads the next scene in the build index and resumes normal time flow.
Time.timeScale
to 1.SceneManager.LoadScene
.
void RestartGame()
Restarts the current level by reloading the active scene.
void ReturnToMenu()
Loads a scene named "Main Menu"
and resets game time.
This script expects the following UI hierarchy for full functionality:
GameOver_Canvas
└── game_over bg
├── GameOverText (TMP_Text)
├── NextLevel (GameObject)
└── Restart (GameObject)
FindObjectOfType<GameOverManager>().ShowGameOver(false); // Trigger failure
FindObjectOfType<GameOverManager>().ShowGameOver(true); // Trigger win
Time.timeScale = 0f
carefully if combining with animation or physics-based sequences."Main Menu"
as a return point.