Update Function of the GameManager Script
Here is a gist that shows the update function of the Game Manager script. The function checks the game state using the switch class. There are four game states in the game, Playing, Death, BeatLevel, and GameOver.
void Update () { | |
switch (gameState) | |
{ | |
case gameStates.Playing: | |
if (playerHealth.isAlive == false) | |
{ | |
// update gameState | |
gameState = gameStates.Death; | |
// set the end game score | |
gameOverScoreDisplay.text = mainScoreDisplay.text; | |
// switch which GUI is showing | |
mainCanvas.SetActive (false); | |
gameOverCanvas.SetActive (true); | |
} else if (canBeatLevel && score>=beatLevelScore) { | |
// update gameState | |
gameState = gameStates.BeatLevel; | |
// hide the player so game doesn't continue playing | |
player.SetActive(false); | |
// switch which GUI is showing | |
mainCanvas.SetActive (false); | |
beatLevelCanvas.SetActive (true); | |
} | |
break; | |
case gameStates.Death: | |
backgroundMusic.volume -= 0.01f; | |
if (backgroundMusic.volume<=0.0f) { | |
AudioSource.PlayClipAtPoint (gameOverSFX,gameObject.transform.position); | |
gameState = gameStates.GameOver; | |
} | |
break; | |
case gameStates.BeatLevel: | |
backgroundMusic.volume -= 0.01f; | |
if (backgroundMusic.volume<=0.0f) { | |
AudioSource.PlayClipAtPoint (beatLevelSFX,gameObject.transform.position); | |
gameState = gameStates.GameOver; | |
} | |
break; | |
case gameStates.GameOver: | |
// nothing | |
break; | |
} | |
} |