Hi,
I’m creating my first simple game using what I have learned from the Unity tutorial projects. My game is working and ends when it should, but the menu that’s supposed to load on game over only pops up about 50% of the time. There are two conditions for the game to end (gameOverOne and gameOverTwo), but how the game ends doesn’t seem have any influence on whether the menu pops up at the end or not.
The menu that appears on game over is similar to the one demonstrated in the Survival Shooter tutorial from Unity. The menu is called using a trigger named “gameOver” in the Animator.
I would really appreciate if someone could take a look at my code and tell me if they see any mistakes that could cause the problem:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class GameController : MonoBehaviour {
public GameObject[] moreStuff;
public Vector3 spawnValues;
public int stuffCount;
public float spawnWait;
public float startWait;
public float waveWait;
public GUIText scoreText;
public Text pointsText;
public Text gameOverText;
public bool gameOverOne;
public bool gameOverTwo;
private bool restart;
private int score;
Animator anim;
void Start ()
{
Time.timeScale = 1;
gameOverOne = false;
gameOverTwo = false;
restart = false;
score = 0;
UpdateScore ();
StartCoroutine (SpawnWaves ());
}
void Awake ()
{
anim = GetComponent <Animator> ();
}
void Update ()
{
if (gameOverOne || gameOverTwo)
{
anim.SetTrigger ("gameOver");
}
}
IEnumerator SpawnWaves ()
{
yield return new WaitForSeconds (startWait);
while (true)
{
for (int i = 0; i < stuffCount; i++)
{
GameObject stuff = moreStuff[Random.Range (0, moreStuff.Length)];
Vector3 spawnPosition = new Vector3 (spawnValues.x, spawnValues.y, spawnValues.z);
Quaternion spawnRotation = Quaternion.identity;
Instantiate (stuff, spawnPosition, spawnRotation);
yield return new WaitForSeconds (spawnWait);
}
if (gameOverOne || gameOverTwo)
{
Time.timeScale = 0.0f;
break;
}
}
}
public void AddScore (int newScoreValue)
{
score += newScoreValue;
UpdateScore ();
}
void UpdateScore ()
{
scoreText.text = "Points: " + score;
pointsText.text = "Total points: " + score;
}
public void GameOverOne ()
{
gameOverText.text = "Sphere!";
gameOverOne = true;
}
public void GameOverTwo ()
{
gameOverText.text = "Square!";
gameOverTwo = true;
}
}