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 /
  • Help Room /
avatar image
0
Question by brlan10 · Dec 10, 2016 at 01:26 AM · c#coroutines

Coroutine not working how I think it would?

This scenario baffles me. Maybe it's because I don't fully grasp coroutines. Disclaimer: I have read the documentation on coroutines and watched the Unity tutorial.

So here's the code in question:

 if (isSubmerged && !isGettingReadyToEmerge)
         {
             
             StartCoroutine(EmergeTimer(1.2f)); //don't call this when isGettingReadyToEmerge==true!
 
         }
 //...
 IEnumerator EmergeTimer(float time) 
     {
 
        
             print("waiting to move... " + frameCount);
             yield return new WaitForSeconds(time);
             print("moving now! - while gettingrdy was " + isGettingReadyToEmerge + " " + frameCount);
             transform.position = playerTransform.position; 
             isGettingReadyToEmerge = true; //statement is here to prevent another call
             
         
     }


Ok, so I would expect the IEnumerator above to never happen while isGettingReadyToEmerge is true. But here's some unity logs that proves me wrong:

alt text

what am I missing?

example.png (44.2 kB)
Comment
Add comment · Show 1
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 getyour411 · Dec 10, 2016 at 02:38 AM 0
Share

Add line 2 / Debug.Log("Calling routine") I think that will help you see how many coroutines you have launched on 4. Also do you have your console in collapse mode?

1 Reply

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

Answer by TBruce · Dec 10, 2016 at 01:39 AM

The following will clear things up for you. (I removed your print commands so you can see what I did easier)

 if (isSubmerged && !isGettingReadyToEmerge)
 {
     StartCoroutine(EmergeTimer(1.2f)); //don't call this when isGettingReadyToEmerge==true!
 }
 
 IEnumerator EmergeTimer(float time) 
 {
     isGettingReadyToEmerge = true; //flag to prevent coroutine from being called while it is being executed
 
     yield return new WaitForSeconds(time);
 
     transform.position = playerTransform.position; 
     isGettingReadyToEmerge = false; // coroutine is finished and can be called again
 }
Comment
Add comment · Show 4 · 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 brlan10 · Dec 10, 2016 at 02:15 AM 0
Share

Thanks for the response, however all this does is prevent the coroutine from getting past the first line.

In your version, isGettingReadyToEmerge is set to true off the bat, preventing the code from entering the coroutine again due to the first if statement. I tested it just in case, and here's what happens:

alt text

To be clear, I want to enter the coroutine, finish it once, then move on with the code and leave that coroutine unused until a later time. In my original scenario, it was getting used once, then calling the end of it over and over.

example2.png (41.0 kB)
avatar image brlan10 · Dec 10, 2016 at 02:38 AM 0
Share

After further contemplation of your answer, I've realized my key misunderstanding of coroutines - I thought that they needed to be called again to resume, but they apparently start a new parallel block of code completely unabated by the function that called it. Was not aware of this. I was able to get the code working by using code that looks like this:

 if (isSubmerged && !isGettingReadyToEmerge && enter)
         {
             
             StartCoroutine(EmergeTimer(1.2f)); 
 
         }
 //...
  IEnumerator EmergeTimer(float time) 
     {
 
        
      
 
         enter = false;
             yield return new WaitForSeconds(time);
            
 
         isGettingReadyToEmerge = true;
         transform.position = playerTransform.position; 
             
             
        }
avatar image TBruce brlan10 · Dec 10, 2016 at 03:04 AM 0
Share

Coroutines usually take the following form

 IEnumerator $$anonymous$$yCoroutine()
 {
     // enter code that I want to be executed immediately here
     // enter yield statement(s) here - execution is temporarily suspended
     // enter code that I want to be executed before completion of coroutine here
 }

One generally creates a flag to prevent a coroutine from being called while it is being exectuted. The flag is generally set on entry to the coroutine and before exit of the coroutine. The flag is checked before calling the coroutine to make sure that it is O$$anonymous$$ to enter.

In your original code you had the comment

 // don't call this when isGettingReadyToEmerge==true!

hence setting isGettingReadyToEmerge to true on entry to the coroutine and false on exit. There is no reason for the additional variable enter unless you are using isGettingReadyToEmerge somewhere else. If that is the case you could just as well do this

bool allowEntry = true;

if (isSubmerged && allowEntry) { StartCoroutine(EmergeTimer(1.2f)); //don't call this when isGettingReadyToEmerge==true! }

 IEnumerator EmergeTimer(float time) 
 {
     allowEntry = false; //flag to prevent coroutine from being called while it is being executed
 
     yield return new WaitForSeconds(time);
 
     transform.position = playerTransform.position; 
     allowEntry = true; // coroutine is finished and can be called again
 }

unless you also need to check the additional flag.

avatar image brlan10 · Dec 10, 2016 at 10:26 AM 0
Share

The reason I need the additional flag is because a later part of the function actually uses "isGettingReadyToEmerge" - it has to become true only once the coroutine is complete and no sooner. That Is why I included "Enter" as the flag. I'm just learning how these things work. I've been using time.delta time for the past 3 months.

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

251 People are following this question.

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

Related Questions

Yield return statement ends loop execution 0 Answers

I have a coroutine that keeps crashing unity. 1 Answer

Getting which function affects a GameObject 0 Answers

If statements inside coroutine aren't running 2 Answers

Why isn't my coroutine working when I call it from another script. 0 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