• 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
21
Question by sebas77 · Feb 16, 2012 at 07:17 PM · startawakeonenable

OnEnable, Awake, Start order

I use a "framework" class that is set to run before all the other scripts (-300). T$$anonymous$$s class has an Awake method. I also use another normal monobehaviour w$$anonymous$$ch defines the OnEnable method.

Once the application runs, the OnEnable of the second monobehaviour is called before the awake of the framework class. Is t$$anonymous$$s the intended behaviour?

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

8 Replies

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

Answer by Wolfram · Jun 27, 2012 at 03:38 PM

It's rather complicated.

[

Update 5.1.1: I didn't re-test everyt$$anonymous$$ng, but here is what seems to be fixed/different in both 5.x and 4.x (tested 5.1 and 4.6), as opposed to 3.5:

  • The bug that the user-defined execution order is ignored if one (but not all) script has an "OnEnable()" function (=see "EDIT3" below) seems to be fixed.

  • OnLevelWasLoaded() is no longer called before Awake(), and always after OnEnable()

  • The execution order of OnLevelWasLoaded() is still arbitrary and cannot be user-defined

  • OnEnable() is no longer called in successive levels if DontDestroyOnLoad is active.

]

A simple test with 3.5.2 revealed, most concurrent functions (well, at least the ones I tested: Awake, Start, OnEnable, FixedUpdate/Update/LateUpdate) abide by the execution order defined for the scripts. The execution order of OnLevelWasLoaded is not affected by that, and therefore cannot be influenced by the user. T$$anonymous$$s could be considered a bug.

The order of the four methods of a script related to initialization is always:

  • Awake()

  • OnEnable()

  • OnLevelWasLoaded() // (only on scene changes)

  • Start()

However, if your script was disabled in the first place(via Script.enabled=false), t$$anonymous$$s order changes to:

  • OnLevelWasLoaded() // is now called first, before Awake()! (only on scene changes)

  • Awake()

  • [OnEnable()/Start() are not executed until the script is actually enabled]

In addition, note that Awake() and OnEnable() calls are connected/interleaved. Meaning, assuming a particular, user-defined execution order A and B with A*<*B,

  • each individual script of type A will execute its Awake(), immediately! followed by its OnEnabled()

  • then all scripts of type B will do the same

  • then all OnLevelWasLoaded() will be executed, in a (presumably) fixed but unpredictable order (assuming t$$anonymous$$s scene was freshly loaded - otherwise t$$anonymous$$s step is skipped completely)

  • then all Start() will be executed, in the order A,B

In particular, t$$anonymous$$s means that OnEnable() of type A will be executed before Awake() of type B, w$$anonymous$$le OnEnable() of type B will be executed after Awake() of type A. T$$anonymous$$s overview explains it more clearly:

  • Awake() of Type A, instance 1

  • OnEnable() of Type A, instance 1

  • Awake() of Type A, instance 2 // order of instances cannot be influenced

  • OnEnable() of Type A, instance 2 // order of instances cannot be influenced

  • Awake() of Type B

  • OnEnable() of Type B

  • OnLevelWasLoaded() of Type ? // order cannot be influenced

  • OnLevelWasLoaded() of Type ? // order cannot be influenced

  • Start() of Type A

  • Start() of Type B

EDIT: Hm, t$$anonymous$$s is a total mess. If DontDestroyOnLoad() is activated for such a script, t$$anonymous$$s will get even more complicated, and the order changes yet again to:

  • [Awake() is never called again, only the very first time]

  • OnEnable()

  • OnLevelWasLoaded() // as opposed to being called before OnEnable(), when DontDestroyOnLoad() is not activated

  • [Start() is never called again, only the very first time]

EDIT2: In addition, when DontDestroyOnLoad() is active, the user-defined execution order is no longer abided by!, neither by OnEnable(), nor by OnLevelWasLoaded().

EDIT3: WAH! I'm gonna stop testing now, t$$anonymous$$s is a neverending story... As @Noisecrime noticed, there is actually another bug, where user-defined execution order is overriden if the script has an OnEnable() function!

Script A has OnEnable, Script B has not:

  • Awake() of Type A

  • OnEnable() of Type A

  • Awake() of Type B

Script B has OnEnable, Script A has not:

  • Awake() of Type B

  • OnEnable() of Type B

  • Awake() of Type A // after Type B!

Comment
Add comment · Show 9 · 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 yoyo · Dec 21, 2012 at 12:28 AM 7
Share
avatar image yoyo · Jul 11, 2013 at 05:48 PM 4
Share
avatar image myfunkyside · Jul 25, 2014 at 02:08 AM 2
Share
avatar image StarWeaver · Jul 25, 2014 at 04:33 AM 3
Share
avatar image meat5000 ♦ · Jul 02, 2015 at 01:12 PM 0
Share
Show more comments
avatar image
5

Answer by Noise crime · Jun 27, 2012 at 02:08 PM

From the sound of it, t$$anonymous$$s is the 'OnEnable' bug with script execution order. That is I suspect your 'framework' script does not have an OnEnable method in it, in w$$anonymous$$ch case for some odd reason it will get trumped in execution order by the scripts that do, despite any ordering you add to the script execution.

To fix t$$anonymous$$s issue, simply add an OnEnable method to your framework script.

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 Wolfram · Jun 27, 2012 at 03:59 PM 0
Share
avatar image huulong · Sep 08, 2015 at 05:23 PM 0
Share
avatar image Fattie · Mar 07, 2016 at 02:04 PM 0
Share
avatar image Oliver-Bogdan · Apr 28, 2016 at 04:19 PM 1
Share
avatar image
3

Answer by Bunny83 · Feb 17, 2012 at 04:19 PM

OnEnable and Awake are called when the objects / script instance is created. They are propably called from the internal constructor. They are even called before Instantiate returns. There is no way to influence when awake / OnEnabled is called on scene objects. The execution order you can set in Unity only influences the Unity generated events like Start, Update, LateUpdate ...

Avoid code in Awake w$$anonymous$$ch relies on other object. Start is made for t$$anonymous$$s purpose. Start is called after all scene objects are created and all Awake function has been called. In general you should use Start to initialize object dependencies.

Comment
Add comment · Show 7 · 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 sebas77 · Feb 17, 2012 at 06:50 PM 0
Share
avatar image Bunny83 · Feb 19, 2012 at 06:50 AM 0
Share
avatar image Noise crime · Jun 27, 2012 at 02:06 PM 1
Share
avatar image Wolfram · Jun 27, 2012 at 04:05 PM 0
Share
avatar image Bunny83 · Jun 27, 2012 at 06:29 PM 2
Share
Show more comments
avatar image
0

Answer by Ashkan_gc · Feb 17, 2012 at 02:31 PM

The manual is not that clear about it. It mentioned OnEnable after Awake in list order but it only said clearly that all Awakes will be called before any Start. again in summary it only shows Start and Awake and not OnEnabled. but for Awake says it will be called After Instantiation and for OnEnabled it says after object becomes enable so theoratically Awake should be sooner than OnEnable.

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
avatar image
0

Answer by Immanuel-Scholz · Nov 07, 2013 at 01:33 PM

I just submitted another strange bug, where the scripting order gets violated. If you have the following setup in your $$anonymous$$erarchy (where "Prefab" is any prefab instance):

 GameObject
    +- Prefab
       +- GameObject
          +- Prefab
          +- ObjectWithUnmportantComponent
 ObjectWithImportantComponent

Then the component of "ObjectWithUnmportantComponent" gets executed first, even if it has a script order number $$anonymous$$gher than a component in "ObjectWithImportantComponent".

Comment
Add comment · Show 2 · 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 davekalina · Dec 03, 2013 at 08:07 PM 0
Share
avatar image Immanuel-Scholz · Dec 03, 2013 at 10:37 PM 0
Share
  • 1
  • 2
  • ›

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

22 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

Related Questions

Execution Order of Scripts - Awake and OnEnable 1 Answer

Should OnEnabled() be changed to be called after Start()? 1 Answer

If a script is attached to more than one gameObject, will Start() & Awake() run more than once? 2 Answers

Does the access modifier of Start(), Awake(), OnEnable() make a difference to Unity? 3 Answers

How to reference GameObject in Awake or Start 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