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 /
avatar image
4
Question by YoungDeveloper · Oct 14, 2015 at 06:44 AM · awakeonenableexecution-order

OnEnable called before Awake

I had some weird behavior in my code which took some time to find the problem. I have two scripts, one works with the data OnEnable, other on Awake, i added debugs to see execution order.

 public class Player : MonoBehaviour {
 
     private void OnEnable(){
         Debug.Log ("registering events");
     }
 }

 public class InputManager : MonoBehaviour{
     private void Awake(){
         Debug.Log("awake");
     }
 }

 

Console

alt text

How is this possible? Docs clearly show that OnEnable is called after Awake. All gameobjects components live on are active and components enabled.

Project came from other dev, is it possible he changed execution order (not sure if thats possible). Ive never seen anything like this.

Edit:

I just created two empty scripts A and B with only this code in each of them, each on its own dummy gameobject, execution order is correct for them. I even attached these components to same gameObjects, execution order is still correct, but reversed on old scripts!

8529368469e03851f63a6bfde3fb50cd.png (8.7 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 KEMBL · Jan 14, 2016 at 08:49 PM 0
Share

Use Start() if possible - it will executed after all scene objects Awakes and OnEnables have been called.

2 Replies

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

Answer by Garazbolg · Oct 14, 2015 at 08:31 AM

Actually Unity doesn't do all awake() and then all OnEnable(). For each script that it loads he does Awake() OnEnable and move on to the next script. So your problem is that Unity load Player first and then InputManager.

alt text

But there is a way to fix this built in Unity : The script Execution Order that you can find under Edit/ProjectSettings/Script Execution Order ( http://docs.unity3d.com/Manual/class-ScriptExecution.html ).

Hope that helped ya figure it out.


unityawakeonenable.png (10.5 kB)
Comment
Add comment · Show 1 · 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 jdelcin · Feb 11, 2018 at 05:08 PM 1
Share

There is a new link https://docs.unity3d.com/$$anonymous$$anual/class-$$anonymous$$ono$$anonymous$$anager.html

avatar image
1

Answer by jdelcin · Feb 11, 2018 at 05:55 PM

The order in which the scripts are placed in the "Hierarchy" affects their execution order as well. Meaning moving the object with script attach up on the hierarchy in the editor will determine what executes before and after it.

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 KarolRynasiewicz · Oct 22, 2018 at 04:04 PM 0
Share

I know it's an old post, but could you please share a link with information you wrote?

avatar image phobos2077 KarolRynasiewicz · May 03, 2019 at 05:02 PM 1
Share

I just confirmed this in my tests. You can create a clean empty scene and test this yourself with some debug scripts.

avatar image Linaran · Jun 29, 2020 at 12:24 AM 0
Share

If this behavior isn't documented it can be changed at some point and break the project.

avatar image guneyozsan · Mar 17, 2021 at 09:33 PM 1
Share

Do not rely on this! Script execution order can be different even between editor and its builded app. Design your app as if it is random.

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

35 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

Related Questions

OnEnable not called after all Awake and not all OnDisable before OnDestroy? 1 Answer

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

OnEnable and Awake depreciated? 1 Answer

Reliable way to detect when the game starts playing on a ScriptableObject? 1 Answer

Execution Order of Scripts - Awake and OnEnable 1 Answer


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