Survive
Part of the Game Architecture in Unity section of Coddy's Unity journey. Lesson 59 of 64.
Challenge
MediumEnd the run when the hero falls.
Heroraises a newpublic event Action Diedafter turning itself off.GameManager.OnEnablecounts kills onEnemy.Diedand calls game over onhero.Died;OnDisableunsubscribes.StarthidesgameOverPanel.- Game over sets
Time.timeScaleto 0 and shows the panel,resultTextreadingWave 2, 7 kills.restartButtonsets 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
1Components, Not Hierarchies
Small ComponentsA Base Enemy ScriptAbstract ComponentsComponents Working TogetherRecap: Modular TurretRecap: Twin Cannon4Serialized Data
Serializable ClassesLists in the InspectorEnums in the InspectorRecap: Loot TableRecap: Roll the Loot2Interfaces in Unity
IDamageableInterfaces and TryGetComponentGetComponentsRecap: Interact KeyRecap: Nearest in ReachPractice on your own: Unity playground