Today we implemented our game flow using another singleton, the GameManager, to coordinate this journey.
Part 1: Game Flow
Previously, we mapped out our game flow so that we know what states we intend to use, and how we can move between them. We implemented some (but not all) of these steps, and those that remain are your responsibility for this week’s assignment.

- We start in an inert state that invites us to “Press S to Start”. This is the Menu state and eventually we will replace it with a separate scene with way better UI.
- Pressing S takes us to a new game, where the number of lives are set, and the objects for the first round are prepared. Once ready, we will go into the GetReady state and hold there momentarily.
- Upon completion of the GetReady state, we move into Playing,
- During Playing, the player will have control of the ship and be able to shoot, and the enemies will begin their attack. There are three possible ways to exit this state.
- The first way occurs in case of a win condition – the player destroys all of the enemy ships – and are taken to the GameOver state with a win message.
- The other two methods are losing conditions – the player object is destroyed by an enemy, or the enemy ship reaches the ground. In this case, the player loses a life, and the round is reset after an oops state, or the game is over if the player has run out of lives.
The first step to build out this structure is for us to create our GameManager object (again created from an empty gameobject), and create the GameManager.cs script for it.
We declare our variable to hold the Singleton…
public static GameManager game; // set up the singleton
… and we assign it. This time, we add in some extra protection to make sure that a version of this singleton does not already exist. This can happen when we move between scenes, if we were to preserve our GameManager object, we might enter a scene that already has a GameManager inside of it. We will test for the instance and destroy if we find one already in place.
private void Awake()
{
if (game)
{
// the singleton already exists, destroy it
Destroy(this.gameObject);
} else
{
game = this;
}
}
Next we define a number of enumerated values for our various gamestates:
public enum GameState { None, StartMenu, GetReady, Playing, Oops, GameOver}
To implement Step 1, we set up a GameState variable in our script and test against it at appropriate times. For instance, pressing “S” would mean one thing during our Menu stage (it should be interpreted as the “start” command) but might mean something different during gameplay, if we are using our Input.GetAxis(“Horizontal”), which relies on pressing WASD keys. And for the assignment, you can do the same sort of check for the GameOver state looking for a user to press the R key to restart.
void Update()
{
// is it time to start a game
if (currentState == GameState.StartMenu)
{
// is the S key pressed?
if (Input.GetKeyDown(KeyCode.S))
{
// start a new game
StartNewGame();
}
}
else if (currentState == GameState.GameOver)
{
// listen for the R reset command
}
}
We can begin to set up the stages. We created a StartNewGame() function that resets the lives left (and later will also reset the score, which is part of this week’s assignment)
We also create ResetRound( ) and StartRound( ) functions. ResetRound( ) is Step 2, and will place the block of enemies in the starting position. We make our Mothership object a prefab, and remove it from the scene, but add the prefab to a GameObject variable in this script. ResetRound instantiates the prefab and stores it in another GameObject variable that is our “currentMothership”. Before we instantiate, we test to see if one already exists. If this returns true, then we probably have arrived here after a player lost a round. We want to reset the enemy objects, so we destroy whatever already exists.
private void ResetRound()
{
// destroy the old mothership if there is one
if (currentMotherShip) {
Destroy(currentMotherShip);
}
// spawn the new one
currentMotherShip = Instantiate(motherShipPrefab);
// go to the get ready state
StartCoroutine(GetReady());
}
Next we transition to the GetReady state, and kicking off a coroutine that will let us pause for 3 seconds, then trigger StartRound( ), which completes Step 3. StartRound( ) moves us to the Playing state, and we remain here until it is time to end a round.
Now that we can get to the playing state, it is time to adjust some of our object behaviors, and make them state dependent.
First, we want to restrict the player movement so that they can only move and shoot while we are in the Playing state. To accomplish this, we add a gamestate check in the Player’s update function. By wrapping the contents with the following “if” statement, we ensure that motion only occurs while in the Playing state.
if (GameManager.game.currentState == GameState.Playing) {
....
}
Again, note that there is no need to associate the game manager with any object in our Player script, and no references to be made. “GameManager.game” is available to be called globally.
The next problem is that our enemy object starts moving and shooting during the get ready state. I also don’t like that it continues to move after my player has been destroyed. I want to prevent the possibility of multiple state requests coming in while we are in our Oops state, so I am going to create new scripts in the Enemy object that will be responsible for starting and stopping the coroutines.
Since I am instantiating the Mothership in GameManager, I have access to the object that is the instance in our scene. I add public functions to StartTheAttack( ) [which starts the coroutines previously held in Start( )] and StopTheAttack( ) in the MotherShip.cs script, and then call those from GameManager using:
currentMotherShip.GetComponent<MotherShipScript>().StopTheAttack();
In our StopTheAttack( ) function, we call the StopAllCoroutines( ) function. This will cease any coroutines currently running from the component.
public void StopTheAttack()
{
StopAllCoroutines();
}
Next, we implemented one of the three conditions that will get us out – specifically the case where an enemy bomb hits the player. We created a PlayerDestroyed( ) function and then call it from a OnCollisionEnter that we add to the Player script.
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "EnemyBomb")
{
// destroy bomb
Destroy(collision.gameObject);
//destroy ourselves
Destroy(this.gameObject);
// tell the game manager we have been destroyed
GameManager.game.PlayerDestroyed();
}
}
Now when the Player collides with an EnemyBomb, the PlayerDestroyed( ) script is called on the GameManager. PlayerDestroyed adjusts the music, and then moves us to the Oops state, through a coroutine.
public void PlayerDestroyed()
{
// make the explosion noise
SoundManager.S.MakePlayerExplosion();
// begin the oops state
StartCoroutine(OopsState());
}
private IEnumerator OopsState()
{
// go into the oops gamestate
currentState = GameState.Oops;
// reduce the lives by 1
livesRemaining--;
// tell the mothership to stop the attack
currentMotherShip.GetComponent<MotherShipScript>().StopTheAttack();
// pause for 2 seconds
yield return new WaitForSeconds(2f);
// decide if we reset or if the game is over
if (livesRemaining > 0)
{
// we live to play another day, reset theround
ResetRound();
} else
{
// go to the game over state
currentState = GameState.GameOver;
}
}
We start the OopsState coroutine by putting the game into the “oops” gamestate, and we the Mothership to stop moving and dropping bombs. Then we yield activity for a few seconds to pause the action. During this time, each object’s Update( ) command will be called on each frame. This is why we move into a different game-state, and have objects test against that state – because the game engine keeps running even when our game flow is taking a rest. We use these variables to help the other objects exhibit the proper behavior.
GameManager.cs
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
public enum GameState { None, StartMenu, GetReady, Playing, Oops, GameOver}
public class GameManager : MonoBehaviour
{
// game variables
public GameState currentState = GameState.None;
public int score = 0;
public int livesRemaining = 0;
private int LIVES_AT_START = 3;
// game objects
public GameObject motherShipPrefab;
private GameObject currentMotherShip;
// UI objects
public Text messageOverlay;
// Singleton Variable
public static GameManager game;
private void Awake()
{
if (game)
{
// the singleton already exists, destroy it
Destroy(this.gameObject);
} else
{
game = this;
}
}
void Start()
{
// set the gamestate to start
currentState = GameState.StartMenu;
// set up the press s to start message
}
void Update()
{
// is it time to start a game
if (currentState == GameState.StartMenu)
{
// is the S key pressed?
if (Input.GetKeyDown(KeyCode.S))
{
// start a new game
StartNewGame();
}
}
else if (currentState == GameState.GameOver)
{
// listen for the R reset command
}
}
private void StartNewGame()
{
// reset the score
score = 0;
// reset the lives
livesRemaining = LIVES_AT_START;
// turn off the start message
// start the round
ResetRound();
}
private void ResetRound()
{
// destroy the old mothership if there is one
if (currentMotherShip) {
Destroy(currentMotherShip);
}
// spawn the new one
currentMotherShip = Instantiate(motherShipPrefab);
// spawn a player
// go to the get ready state
StartCoroutine(GetReady());
}
private IEnumerator GetReady()
{
// set the state
currentState = GameState.GetReady;
// start the music
SoundManager.S.startTheMusic();
// Set our screen message
messageOverlay.text = "Get Ready";
// wait for a few seconds
yield return new WaitForSeconds(2f);
messageOverlay.text = "running";
// start playing
StartRound();
}
private void StartRound()
{
// tell the mother ship to start
currentMotherShip.GetComponent<MotherShipScript>().StartTheAttack();
// gamestate changes to playing
currentState = GameState.Playing;
}
public void PlayerDestroyed()
{
// explode the player and stop the music
SoundManager.S.MakePlayerExplosion();
// oops state
StartCoroutine(OopsState());
}
private IEnumerator OopsState()
{
// sett the gamestate
currentState = GameState.Oops;
// reduce my life count
livesRemaining--;
// tell the mothership to stop the attack
currentMotherShip.GetComponent<MotherShipScript>().StopTheAttack();
// pause for 2 seconds
yield return new WaitForSeconds(2f);
// do we reset the game?
if (livesRemaining > 0)
{
ResetRound();
} else
{
// game is over
currentState = GameState.GameOver;
messageOverlay.text = "GameOver";
}
}
}
PlayerScript.cs
using UnityEngine;
public class PlayerScript : MonoBehaviour
{
public float speed; // max speed of movement
public float MAX_OFFSET; // how far can the player move from center
public GameObject bulletPrefab;
// Update is called once per frame
void Update()
{
if (GameManager.game.currentState == GameState.Playing)
{
MovePlayer();
}
}
private void MovePlayer()
{
Vector3 currentPosition = transform.position; // get our current location
// move the player object horizontally
currentPosition.x = currentPosition.x + (Input.GetAxisRaw("Horizontal") * speed * Time.deltaTime);
// clamp our values
currentPosition.x = Mathf.Clamp(currentPosition.x, -MAX_OFFSET, MAX_OFFSET);
// return the final position
transform.position = currentPosition;
// check for firing
if (Input.GetKeyDown(KeyCode.Space))
{
FireBullet();
}
}
private void FireBullet()
{
Instantiate(bulletPrefab, transform.position + Vector3.up, Quaternion.identity);
}
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "EnemyBomb")
{
// destroy bomb
Destroy(collision.gameObject);
//destroy ourselves
Destroy(this.gameObject);
// tell the game manager we have been destroyed
GameManager.game.PlayerDestroyed();
}
}
}
MotherShipScript.cs
using System.Collections;
using UnityEngine;
public class MotherShipScript : MonoBehaviour
{
public int stepsToSide;
public float sideStepUnits;
public float downStepUnits;
public float timeBetweenSteps;
public float timeBetweenBombs;
public bool _isRunning = false;
public void StartTheAttack()
{
StartCoroutine(MoveMother());
StartCoroutine(SendABomb());
}
public void StopTheAttack()
{
StopAllCoroutines();
}
public IEnumerator MoveMother()
{
// define our move vectors
Vector3 sideVector = Vector3.right * sideStepUnits;
Vector3 downVector = Vector3.down * downStepUnits;
// move our enemies until there are no enemies left
while(transform.childCount > 0)
{
// step one: move sidestep units to the side, do this x times
for(int i = 0; i < stepsToSide; i++)
{
// move sidestep
transform.position += sideVector;
// wait for the next move
yield return new WaitForSeconds(timeBetweenSteps);
// verify that we are all still here
if (transform.childCount <= 0)
{
Debug.Log("All Enemies Destroyed");
SoundManager.S.stopTheMusic();
StopTheAttack();
}
}
// step two: move downstep units down, do this once
transform.position += downVector;
// wait for the next move
yield return new WaitForSeconds(timeBetweenSteps);
// switch direction
sideVector *= -1f;
}
Debug.Log("Mothership has no children");
_isRunning = false;
}
public IEnumerator SendABomb()
{
// record that we are running
_isRunning = true;
while (_isRunning)
{
// how many children do I have?
int enemyCount = transform.childCount;
// if we have children
if (enemyCount > 0)
{
// pick one at random
int enemyIndex = Random.Range(0, enemyCount);
// get that object
Transform thisEnemy = transform.GetChild(enemyIndex);
// get the enemy script
EnemyScript thisEnemyScript = thisEnemy.GetComponent<EnemyScript>();
// check to make sure there is a valid enemyscript
if (thisEnemyScript)
{
// drop the bomb
thisEnemyScript.DropABomb();
}
} else
{
// no more children, stop running
_isRunning = false;
}
yield return new WaitForSeconds(timeBetweenBombs);
}
}
}