• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by DKitch9 · May 28, 2017 at 01:57 PM · c#unity 5scripting problem2d gamelogic

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!!!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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);
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DKitch9 · May 28, 2017 at 10:20 PM 0
Share

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.

avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Coroutines randomly stop working 0 Answers

A* pathfinding for dynamic obstacles and player made blockages? 0 Answers

Add audio. 1 Answer

why tansform.Translate is not working? 1 Answer

Built project, now scripts are missing. 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges