Menu
CoddyTech

Survive

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

challenge icon

Challenge

Medium

End the run when the hero falls.

  1. Hero raises a new public event Action Died after turning itself off.
  2. GameManager.OnEnable counts kills on Enemy.Died and calls game over on hero.Died; OnDisable unsubscribes. Start hides gameOverPanel.
  3. Game over sets Time.timeScale to 0 and shows the panel, resultText reading Wave 2, 7 kills. restartButton sets it to 1 and reloads.

Press Play and fight until you fall. Then Check.

Try it yourself

using System.IO;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using TMPro;

public class GameManager : MonoBehaviour
{
    [SerializeField] private Hero hero;
    [SerializeField] private WaveSpawner spawner;
    [SerializeField] private GameObject gameOverPanel;
    [SerializeField] private TMP_Text resultText;
    [SerializeField] private TMP_Text bestText;
    [SerializeField] private Button restartButton;

    // A later lesson fills this in: the end of the run and the best run.
}

All lessons in Game Architecture in Unity

Practice on your own: Unity playground