The GameTimer
component manages countdown timing during a level. It updates the on-screen UI with the remaining time and notifies the GameOverManager
when the timer reaches zero, triggering a game-over state. This script is designed to provide a centralized and reliable time control mechanism for levels with strict timing constraints.
GameTimer
Namespace: global
Inherits from: MonoBehaviour
float timeRemaining = 6000f
The total amount of time (in seconds) remaining for the level. This value is decreased in real-time as the game progresses.
TMP_Text timerText
Reference to a TextMeshPro UI element where the countdown is displayed to the player.
bool timerRunning = true
Indicates whether the timer is actively counting down.
GameOverManager gameOverManager
Reference to the scene’s GameOverManager
, used to trigger the game over state when time expires.
void Start()
Initializes internal references and updates the UI with the starting time.
GameOverManager
in the scene.
void Update()
Continuously decreases the timer while timerRunning
is true
. If the timer reaches zero, it triggers a timeout sequence.
Time.deltaTime
from timeRemaining
.UpdateTimerUI()
to refresh the countdown display.TimeOut()
if time reaches zero.
void UpdateTimerUI()
Formats the timer value and updates the timerText
element with the remaining time, rounded up to the nearest second.
void TimeOut()
Stops the timer, ensures the UI shows zero, and triggers a loss via GameOverManager
.
timerRunning
to false
.timeRemaining
to 0
.ShowGameOver(false)
to indicate a failed attempt.
Attach this script to a GameObject in your scene and assign a TMP_Text
reference in the Inspector:
// Optional external check
if (FindObjectOfType<GameTimer>().timeRemaining < 5f) {
Debug.Log("Almost out of time!");
}
Mathf.Ceil
to ensure timer countdown feels fair to players.GameOverManager
and timerText
via Inspector or runtime assignment.timeRemaining
with other game systems (e.g. FuseBurner.maxBurnTime
) for consistent difficulty scaling.