Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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.

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

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges