Part 7: Game Over

Christiane Snyder

We want the game to end when the player hits a spaceship, so we’ll start by creating an EndGame() function inside of GameManager.cs that will be called when the player hits a spaceship obstacle.


Before ending the game, it will delete all powerups and spaceships in the scene and clear the lists that act as a records of active obstacles/power-ups.


It will then set the IsPlaying boolean to false, which will indicate a GameOver/Idle state in the GameManager. Right now, setting IsPlaying to false will only stop the GameManager from spawning more objects for the duration it's false, but we will soon add in more components of the game that will be dependant on this boolean.


Add the following code to the GameManager script’s RemoveObject function. These additional lines will check if a hit GameObject is a spaceship (it’s tag is ‘SpaceShip’) and call our newly created EndGame() function if it is.


Now that our game can end, let’s add in a message to the player that they have lost.

Create a new 3D Text object named GameOverText will the following attributes:



Create a private GameObject variable also named GameOverText inside GameManager.cs and initialize it to the text you just created by finding it in the scene by name.

Set it to inactive in the GameManager’s  Start() function so that it does not display itself when the game begins.



Finally, reactivate it in the EndGame() function.