Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 metroidsnes · Mar 09, 2015 at 12:17 PM · coroutineupdate

Coroutine is not called between frames

I start a coroutine in Start() like this: StartCoroutine("CoroutineName") and inside it I use yield return null to wait for another frame. But when I log calls to Update() and the coroutine, this is what I get:

alt text

Coroutine is not called between first two Update() calls. Why?

[EDIT]

Example code:

  private void Start() {
      Debug.Log("Start()");

      StartCoroutine("ExampleCoroutine");
  }

  private IEnumerator ExampleCoroutine() {
      Debug.Log("Called for the first time");

      while (true) {
          yield return null;
      }
  }

  private void Update() {
      Debug.Log("Update()");
  }

Extended log:

alt text

coroutine-calls2.png (26.2 kB)
coroutine-calls.png (24.3 kB)
Comment
Add comment · Show 4
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 fafase · Mar 09, 2015 at 12:46 PM 0
Share

Can you print something in the Start to see when it gets called. I would assume Coroutine yield instruction is a debug, you should also add one after the yield. With all that, it gets easier to see the process.

avatar image metroidsnes · Mar 09, 2015 at 01:26 PM 0
Share

I added new info to the question.

avatar image Bonfire-Boy · Mar 09, 2015 at 01:30 PM 0
Share

Where are the "yield return null" lines in your log co$$anonymous$$g from?

avatar image metroidsnes · Mar 09, 2015 at 01:35 PM 0
Share

I have a breakpoint that prints a message when hit. alt text

breakpoint.png (2.7 kB)

2 Replies

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

Answer by fafase · Mar 09, 2015 at 01:56 PM

Considering your new addition, there is nothing wrong.

First you get the Start printing which also starts the coroutine. The coroutine runs until the yield and returns to the Start that finishes. We get:

  Start
  StartCoroutine
  Coroutine for first time
  yield  return null
  (End of Start after the coroutine call)
  Update

The first update is called as should. The frame is now done. Next frame

Coroutines are called after Update so the next frame update is called and finishes. The compiler finds a pending coroutine and runs it until it hits a yield or the end:

  Update
  yield return null

now all together:

  Start
  StartCoroutine
  Coroutine for first time
  yield  return null
  (End of Start after the coroutine call)
  Update
  Update
  yield return null

Coroutines are called after Update so the next frame update is called and finishes. The compiler finds a pending coroutine and runs it until it hits a yield or the end:

  Update
  yield return null

now all together:

  Start
  StartCoroutine
  Coroutine for first time
  yield  return null
  (End of Start after the coroutine call)
  Update
  Update
  yield return null
  Update
  yield return null

Coroutines are called after Update so the next frame update is called and finishes. The compiler finds a pending coroutine and runs it until it hits a yield or the end:

  Update
  yield return null

now all together:

  Start
  StartCoroutine
  Coroutine for first time
  yield  return null
  (End of Start after the coroutine call)
  Update
  Update
  yield return null
  Update
  yield return null
  Update
  yield return null

... And so on

Comment
Add comment · Show 5 · 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 Bonfire-Boy · Mar 09, 2015 at 02:06 PM 0
Share

But what about coroutines being called after that first call to Update()? Do coroutines not get called after the Update() of the frame in which they were started?

avatar image fafase · Mar 09, 2015 at 02:16 PM 0
Share

Updated the answer and read it again as I explained why there is no call to coroutine after the first update.

avatar image metroidsnes · Mar 09, 2015 at 03:14 PM 0
Share

@fafase Thanks for explanation. I solved my problem by starting coroutine after first Update().

 private int frame;
 private void Update() {
     frame++;
 
     if (frame == 2) StartCoroutine("ExampleCoroutine");
 }                        
avatar image Bonfire-Boy · Mar 09, 2015 at 03:31 PM 1
Share

@fafase It's still not made clear why the coroutine doesn't get called after the first update. Specifically, you say...

"The first update is called as should. The frame is now done. Next frame"

This is problematic because, as you, show, generally speaking after an update the frame is not done, but ins$$anonymous$$d the engine (not the compiler I$$anonymous$$O) looks for pending coroutines and runs them.

I believe the answer is that because the coroutine in question has been run in that frame (when it was started), its "pending flag" has been cleared (and won't be reset till the start of the next frame). To me this seems like a crucial point to complete your otherwise excellent answer.

avatar image fafase · Mar 09, 2015 at 04:13 PM 0
Share

Ooooh ok I see now. Well yes, coroutine should not run twice so I guess it stores it for next frame.

avatar image
0

Answer by alok-kr-029 · Mar 09, 2015 at 12:24 PM

Try

IEnumerator Example() { yield return new WaitForEndOfFrame(); }

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 metroidsnes · Mar 09, 2015 at 12:41 PM 0
Share

Tried it, exactly the same result, no coroutine call between two first Update() calls.

avatar image alok-kr-029 · Mar 09, 2015 at 12:47 PM 0
Share

its clear from the png that For first time the Update method is called twice in one frame I think so its due to its your function name as Update method once called automatically due to its behaviour and other due to your coroutine ,hence its called twice . I would suggest you to try changing the name of your function that you are calling

How are you calling it can you show the code

avatar image metroidsnes · Mar 09, 2015 at 01:26 PM 0
Share

I added new info to the question.

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

How to slow down large code? 1 Answer

void update working under conditions 1 Answer

Coroutine in Extension Method 1 Answer

WebGL prevent Unresponsive script warning 1 Answer

Load Level when dead for 1 second 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