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
0
Question by cgraf1 · May 04, 2017 at 06:16 PM · booleancoroutinesupdate functionsubtracting

Game randomly subtracts 2 lives when it is only supposed to subtract 1

In my game, objects fall towards the bottom of the screen and the player has to shoot them out. In Lives mode, if they pass the bottom of the screen, 1 life is supposed to be subtracted and all other objects one the screen are destroyed to sort of reset and give the player a second.

Every once in awhile it will subtract 2 lives instead of just 1.

This Update checks if an object has gone past the bottom of the screen.

 void Update () {
 
         isColliding = false;
 
         Vector2 pos = transform.position;
 
         pos = new Vector2 (pos.x, pos.y - speedY * Time.deltaTime);
 
 
         transform.position = pos;
 
         Vector2 min = Camera.main.ViewportToWorldPoint(new Vector2 (0,0));
 
         if(transform.position.y < min.y - 0.5){
 
             if(!isFalling){
                 if(ClickType.gameType == 2){
                     gameObject.GetComponent<AudioSource>().clip = oops;
                     gameObject.GetComponent<AudioSource>().Play();
                 }    
                 isFalling = true;
             }
 
             StartCoroutine(OhNo());
         }
 
     }
 

This is what performs when an object does go past the screen

 IEnumerator OhNo(){
         
         yield return new WaitForSeconds(0.8f);
         Destroy(gameObject);
         if(ClickType.gameType == 2){
             GameObject[] fallingShapes = GameObject.FindGameObjectsWithTag("FallingShape");
             foreach(GameObject fS in fallingShapes){
                 GameObject.Destroy(fS);
             }
             Timer.livesRemaining -= 1;
             Debug.Log("Lives are:" + Timer.livesRemaining);
         }
     }

Any and all help would be much appreciated. Thanks very much!

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

1 Reply

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

Answer by FortisVenaliter · May 04, 2017 at 07:43 PM

When the object goes below the bottom of the screen, you are starting a new coroutine in the Update function, e.g. every frame that it still exists below the screen.

Since the coroutine destroys the object immediately after running, in most cases it will only actually run the code after WaitForSeconds once. But the Destroy command kills the object at the end of the frame, not right away. And since floating points can be imprecise, it's possible that on uncommon occurences, two will finish in the same frame, effectively running it twice. To confirm this, add a Debug.Log() to the coroutine function before WaitForSeconds. It will likely log about 48 times per block (0.8 * 60FPS).

What you probable want to do is have a class-scope boolean variable called 'isDestroying' or something. Check that it's false before starting the coroutine, and set it to true after you start the coroutine the first time. That will ensure it's only being called once.

Comment
Add comment · Show 3 · 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 cgraf1 · May 04, 2017 at 08:09 PM 0
Share

Thank you @FortisVenaliter. I did the Debug test and you were right. I then added a new bool and it only came up once, fixing the issue. It is funny I have used this bool technique before with collisions, and thought my isFalling bool was taking care of this, but it appears I needed another. Thanks again!

avatar image FortisVenaliter cgraf1 · May 04, 2017 at 08:31 PM 0
Share

Well, it may have, but your StartCoroutine call is not within the isFalling block.

avatar image cgraf1 FortisVenaliter · May 05, 2017 at 05:35 PM 0
Share

Yes. Looking back that was actually to prevent the audio cue from playing each frame. Thanks again

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

WaitForSeconds using Update? 1 Answer

Multiple coroutines starting from Update() freeze/crash Unity 0 Answers

Stuck while solving a Coroutine problem 0 Answers

How can I make this only act once? 3 Answers

Change Bool When Audio Clip Ends C# 1 Answer


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