• 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 brunoleos · Jul 17, 2017 at 04:54 PM · coroutinemonobehaviourienumeratorstartorder-of-execution

Execution order when Start is declared with IEnumerator return (as a Coroutine)

I could find in the documentation the event execution order of Start and Update, which says:

Start: Start is called before the first frame update only if the script instance is enabled.

For objects added to the scene, the Start function will be called on all scripts before Update, etc are called for any of them.

As I understand it, when loading a scene, Update will only be called after all Start functions are called on all GameObjects.

What about the case where Start is a Coroutine (declared with IEnumerator return)? Will Update be called after all Start are called, or after all Start are finished?

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

Answer by brunoleos · Jul 17, 2017 at 04:58 PM

I've done a simple test using this script in two GameObjects:

 public class StartAsCoroutine : MonoBehaviour
 {
     private void Awake()
     {
         StartCoroutine(Step("Awake"));
     }
 
     private IEnumerator Start()
     {
         yield return StartCoroutine(Step("Start"));
     }
 
     private void Update()
     {
         Debug.Log(gameObject.name + ": " + "Update" + " frame " + Time.frameCount);
     }
 
     private IEnumerator Step(string method)
     {
         while(true)
         {
             Debug.Log(gameObject.name + ": " + method + " frame " + Time.frameCount);
             yield return 0;
         }
     }
 }

The result is that Update is called in the same frame that all Start are also called. Unity doesn't wait for the Start coroutine completion to begin calling Update. The calls to the coroutines are done after the Update calls, as expected.

With this behaviour, it makes no sense for me to having Start as a coroutine, as I could StartCoroutine at void Start().

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

70 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

Related Questions

Call function after other objects have run their Start() function 2 Answers

Coroutine start and stop 2 Answers

Update scene every 5 minutes 2 Answers

3rd person controller in c#? 1 Answer

yield return new WaitForSeconds () not working 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges