Menu
CoddyTech

The HUD

Part of the Game Architecture in Unity section of Coddy's Unity journey. Lesson 58 of 64.

challenge icon

Challenge

Medium

Build a HUD that only listens.

  1. Hero gets public event Action<int, int> HealthChanged; raise it with (health, maxHealth) in Start and on every change.
  2. WaveSpawner gets public event Action<int> WaveStarted, raised with Wave as each wave starts.
  3. In HUD, subscribe in OnEnable, unsubscribe in OnDisable; fill healthBar, show HP 3/5 and Wave 2. No Update.

Press Play and fight: the HUD keeps up. Then Check.

Try it yourself

using UnityEngine;
using UnityEngine.UI;
using TMPro;

public class HUD : MonoBehaviour
{
    [SerializeField] private Hero hero;
    [SerializeField] private WaveSpawner spawner;
    [SerializeField] private Image healthBar;
    [SerializeField] private TMP_Text healthText;
    [SerializeField] private TMP_Text waveText;

    // A later lesson fills this in: health and wave on screen.
}

All lessons in Game Architecture in Unity

Practice on your own: Unity playground