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
3
Question by albertwang · May 24, 2012 at 07:56 AM · coroutineexecution order

Coroutine, Start() and Update() execution order

Hi, I'm newbie in unity programming, here is one simple test program I wrote which I got a bit confusing on the execution orders:

    public float startbegintime = 0;
    public float startendtime =0 ;
    public float updatestarttime = 0;
 
   public int commonvar = 0;
   public int updatecnt =0;

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

     startbegintime = Time.time;
     StartCoroutine(myFunc());
     startendtime = Time.time;

     Debug.Log("Start end");
 }
 
 
 IEnumerator myFunc()
 {
     for(int i = 0; i<100; i++)
     {
         Debug.Log("myFunc Loop "+(i+1).ToString());    
         commonvar++;
         
         yield return 0;
     }        
 }

 void Update () {
     
     if(updatestarttime == 0)
     {
         updatestarttime= Time.time;
         Debug.Log("Update started");
     }
     
     if(updatecnt<100)
     {
         commonvar++;
         updatecnt++;
         Debug.Log("Common var is "+commonvar.ToString());
     }
 }



Well, the console output is as follows:

  Start begin
  myFunc Loop 1
  Start end
  Update started
  Common var is 2
  Common var is 3
  myFunc Loop 2
  Common var is 5
  myFunc Loop 3
  Common var is 7
  myFunc Loop 4
 ......

and the time variables values are startbegintime = startendtime = 0, updatestarttime =0.2

so by the output, I noticed that the execution order is like this:

Start begin -> first iteration of coroutine -> Start end -> first update -> second update -> second iteration of coroutine -> third update -> third iteration of coroutine ->......

So why is that there're 2 consecutive update call before the second iteration of coroutine?

I took a look at the documentation on execution order, seems I still can't make it clear. http://unity3d.com/support/documentation/Manual/Execution%20Order.html

which says:

Normal coroutine updates are run after the Update function returns.

Anybody has some ideas on this? Thanks!

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
7
Best Answer

Answer by whydoidoit · May 24, 2012 at 10:49 AM

Coroutines run every frame after update - but the primary rule here is that it is running once every frame.

On the first frame the coroutine runs because you tell it to - in Start, which is before Update - it will not run again on that frame. Then afterwards it runs in its normal position after Update.

If you want it to start after the first Update then either:

  • Start it in Update the first time

  • Ignore the first time it is called before update by the first line of it being yield return 0 - in this case Update will be called twice before the main body of the coroutine runs

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 albertwang · May 25, 2012 at 06:58 AM 0
Share

Thanks for the clear explanation!

avatar image whydoidoit · May 25, 2012 at 08:24 AM 0
Share

No problem :) Can you mark the question as answered so it disappears from the unanswered list?

avatar image albertwang · May 25, 2012 at 02:19 PM 0
Share

sure, just done.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Does a coroutine invoked using StartCorutine() need to finish before code below executes in a function? 1 Answer

Taking a sequence of screenshots 1 Answer

Starting Coroutine within Start method 1 Answer

Method returning data from WWW 1 Answer

How to stop coroutines or functions 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