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*, transform.position, Quaternion.identity);*

yield return new WaitForSeconds(Random.Range(1.0f, 2.0f));

if (gameOver)
{
GameObject.FindGameObjectWithTag(“MainCamera”).GetComponent().Stop();
GameObject.FindGameObjectWithTag(“Destroyer”).GetComponent().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();
}
if (scoreController == null)
{
Debug.Log(“Cannot find ‘ScoreManager’ script”);
}

GameObject spawnObject = GameObject.FindWithTag(“SpawnBox”);
if (spawnObject != null)
{
spawnController = spawnObject.GetComponent();
}
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!!!

IEnumerator SpawnNewBox()
{
while (!gameOver)
{
yield return new WaitForSeconds(Random.Range(1.0f, 1.1f));
int i = Random.Range(0, boxList.Length);
Instantiate(boxList*, transform.position, Quaternion.identity);*
yield return new WaitForSeconds(Random.Range(1.0f, 2.0f));
}
if (gameOver)
{
GameObject.FindGameObjectWithTag(“MainCamera”).GetComponent().Stop();
GameObject.FindGameObjectWithTag(“Destroyer”).GetComponent().Play();
gameOverText.SetActive(true);
restartButton.SetActive(true);
break;
}
}
Try this

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*, transform.position, Quaternion.identity);*

yield return new WaitForSeconds(Random.Range(2.0f, 3.1f)); // cyclic delay
}
Camera.main.GetComponent().Stop();
GameObject.FindGameObjectWithTag(“Destroyer”).GetComponent().Play();
gameOverText.SetActive(true);
restartButton.SetActive(true);
}