- Home /
Game over funcition isn't being called accurately?
Hello all! I am very close to finishing up my game but there has been this lingering issue...my game over function is not working as it should be. I'm making a color matching game, and when you match the incorrect color it's game over. What's happening though is sometimes when you match the incorrect color, the game continues on for one more color instead of ending immediately as intended. Out of all the testing I have done I can't find a pattern or pin point exactly when this error occurs...it seems to be random. Here are two scripts I have that utilize the game over function in question. The first one is a script I use to spawn new colors. and the second one is on each colored block you have to swipe to match:
using UnityEngine;
using System.Collections;
public class SpawnBox : MonoBehaviour
{
public GameObject[] boxList;
private bool gameOver;
public GameObject gameOverText;
public GameObject restartButton;
void Start()
{
gameOver = false;
StartCoroutine(SpawnNewBox());
}
IEnumerator SpawnNewBox()
{
while (!gameOver)
{
yield return new WaitForSeconds(Random.Range(1.0f, 1.1f));
int i = Random.Range(0, boxList.Length);
Instantiate(boxList[i], transform.position, Quaternion.identity);
yield return new WaitForSeconds(Random.Range(1.0f, 2.0f));
if (gameOver)
{
GameObject.FindGameObjectWithTag("MainCamera").GetComponent<AudioSource>().Stop();
GameObject.FindGameObjectWithTag("Destroyer").GetComponent<AudioSource>().Play();
gameOverText.SetActive(true);
restartButton.SetActive(true);
break;
}
}
}
public void GameOver()
{
gameOver = true;
}
}
and the second one:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class CatchScoreR : MonoBehaviour {
private ScoreManager scoreController;
private SpawnBox spawnController;
// Use this for initialization
void Start () {
GameObject scoreObject = GameObject.FindWithTag("SpawnBox");
if (scoreObject != null)
{
scoreController = scoreObject.GetComponent<ScoreManager>();
}
if (scoreController == null)
{
Debug.Log("Cannot find 'ScoreManager' script");
}
GameObject spawnObject = GameObject.FindWithTag("SpawnBox");
if (spawnObject != null)
{
spawnController = spawnObject.GetComponent<SpawnBox>();
}
if (spawnController == null)
{
Debug.Log("Cannot find 'SpawnBox' script");
}
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Box0")
{
scoreController.Score();
}
else
{
spawnController.GameOver();
}
}
}
Let me know if I need to clarify anything or provide more detail, thank you!!!
Answer by Bunny83 · May 28, 2017 at 04:33 PM
You yield at two points in your coroutine. When "gameOver" get set to true while you currently are waiting at the first yield, the coroutine would create another box since your while loop only exits when the loop re-evaluates it's condition. However if your coroutine waits at the second yield while gameOver turns true the coroutine would leave the while loop when it continues.
I don't see a reason why you actually have two wait times. Maybe you want something like this:
IEnumerator SpawnNewBox()
{
yield return new WaitForSeconds(1.0f); // initial delay
while (!gameOver)
{
int i = Random.Range(0, boxList.Length);
Instantiate(boxList[i], transform.position, Quaternion.identity);
yield return new WaitForSeconds(Random.Range(2.0f, 3.1f)); // cyclic delay
}
Camera.main.GetComponent<AudioSource>().Stop();
GameObject.FindGameObjectWithTag("Destroyer").GetComponent<AudioSource>().Play();
gameOverText.SetActive(true);
restartButton.SetActive(true);
}
Works like a charm! So was my issue the the length of time I was calling on the second yield? I noticed when I took the code you provided, I forgot to change it and left it at 1 - 2 seconds. When I bumped it up to 2 - 3.1 though, I no longer had any issues.
Answer by ismaelnascimento01 · May 28, 2017 at 03:00 PM
IEnumerator SpawnNewBox()
{
while (!gameOver)
{
yield return new WaitForSeconds(Random.Range(1.0f, 1.1f));
int i = Random.Range(0, boxList.Length);
Instantiate(boxList[i], transform.position, Quaternion.identity);
yield return new WaitForSeconds(Random.Range(1.0f, 2.0f));
}
if (gameOver)
{
GameObject.FindGameObjectWithTag("MainCamera").GetComponent<AudioSource>().Stop();
GameObject.FindGameObjectWithTag("Destroyer").GetComponent<AudioSource>().Play();
gameOverText.SetActive(true);
restartButton.SetActive(true);
break;
}
}
Try this
Your answer
Follow this Question
Related Questions
How can I flip only my 'Player' gameobject, and l leave it's child object alone? 2 Answers
How to detect an object which be in FOV of certain camera ? 1 Answer
I need help writing a simple script for making an object face the mouse in 2D. 2 Answers
Multiple Cars not working 1 Answer
Create more text in unity GUI? (C#) 2 Answers