UI: Score, Health, and Level Complete
Wire the systems from earlier lessons to a UI a player can actually see, using a small manager class that everything else talks to.
using UnityEngine;
using TMPro;
public class GameManager : MonoBehaviour
{
public static GameManager Instance { get; private set; }
public TMP_Text scoreText;
public GameObject levelCompletePanel;
int score;
void Awake()
{
Instance = this;
}
public void AddScore(int amount)
{
score += amount;
scoreText.text = $"Score: {score}";
}
public void CompleteLevel()
{
levelCompletePanel.SetActive(true);
Time.timeScale = 0f;
}
}
The pieces
public static GameManager Instance { get; private set; }is a lightweight singleton: any script can reach the oneGameManagerin the scene withGameManager.Instance, instead of every script needing a manually-dragged reference. That's howCoincalledGameManager.Instance.AddScore(...)in the previous lesson.scoreText.text = $"Score: {score}"is the same pattern from the Unity Essentials course, code pushes new values into the UI, the UI never updates itself.levelCompletePanelis aCanvaschild (a panel with a background and text) that starts inactive in the Inspector and getsSetActive(true)only when the level is finished.Time.timeScale = 0fpauses all physics and any code usingTime.deltaTime(like the patroller's movement), a simple way to freeze gameplay behind a completion or pause screen without disabling every script individually.
WARNING
Setting Time.timeScale = 0f also freezes Update()-based countdowns and animations driven by deltaTime. Resetting it back to 1f (e.g. when restarting the level) is easy to forget, and leaves the whole game frozen.