Crash Ends the Run
Part of the Building Real Games section of Coddy's Unity journey. Lesson 31 of 60.
Challenge
MediumA crash should stop the run.
- Above
GameManager, declarepublic enum GameState { Playing, GameOver }; inside, addpublic GameState state = GameState.Playing;. public void GameOver()setsstatetoGameState.GameOverandspeedto 0, and showsgameOverPanel.RunnercallsGameManager.Instance.GameOver()on a trigger taggedObstacle.GameManager,RunnerandSpawnerstartUpdate()with areturnunlessPlaying.
Press Play and crash: everything stops. Then Check.
Try it yourself
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using TMPro;
public class GameManager : MonoBehaviour
{
public static GameManager Instance;
public float speed = 10f;
public float speedUp = 0.5f;
public float maxSpeed = 20f;
public TMP_Text distanceText;
public GameObject gameOverPanel;
public Button restartButton;
private float distance;
void Awake()
{
Instance = this;
}
void Update()
{
speed = Mathf.Min(speed + speedUp * Time.deltaTime, maxSpeed);
distance += speed * Time.deltaTime;
distanceText.text = "Distance: " + Mathf.FloorToInt(distance) + " m";
}
}
All lessons in Building Real Games
2Buttons and Screens
Buttons and onClickLambdasWiring in the InspectorPanels On and OffHealth BarsRecap: Upgrade Shop5Saving with PlayerPrefs
Save a NumberLoad with a DefaultHigh ScoreSave SettingsRecap: Coins in the Bank3The Game Manager
One Script Runs the GameThe SingletonGame StatesSwitching on the StateRecap: Round Manager6Project: Endless Runner
Three LanesObstacles Ahead9The New Input System
Two Input SystemsKeyboard and MousePressed This FrameInput ActionsRecap: Input SwapPractice on your own: Unity playground