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
1
Question by manu3d · Sep 08, 2013 at 01:52 PM · editorplayermonobehaviourorderexecution

MonoBehaviour execution order from Edit to Play mode and back.

How do I check the execution order of MonoBehaviour-derived event functions/methods in the context of switching back and forth from Edit to Play mode and back?

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

2 Replies

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

Answer by manu3d · Sep 08, 2013 at 02:07 PM

The execution order of event functions in a monobehaviour-derived class is mostly detailed here. Also relevant is this page on execution order settings.

But more to the point, a simple script will allow you to track execution order through the console:

 using UnityEngine;
 
 [ExecuteInEditMode]
 public class ExecutionOrderDisplay : MonoBehaviour 
 {
     private string id;
     
     private string checkMode()
     {
         if(Application.isPlaying)
             return "PlayMode";
         else
             return "EditMode";
     }
     
     void Awake() 
     {
         id = GetInstanceID() + "";
         
         Debug.Log(checkMode()+"::ExecutionOrderDisplay::"+id+"::"+name+"::Awake()");    
     }
     
     void OnEnable()
     {
         Debug.Log(checkMode()+"::ExecutionOrderDisplay::"+id+"::"+name+"::OnEnable()");
     }
     
     void Start() 
     {
         Debug.Log(checkMode()+"::ExecutionOrderDisplay::"+id+"::"+name+"::Start()");
     }
     
     void Update() 
     {
         // enabling the next line clogs the console, obviously
         // Debug.Log(checkMode()+"::ExecutionOrderDisplay::"+id+"::"+name+"::Update()");
     }
     
     void OnDisable()
     {
         Debug.Log(checkMode()+"::ExecutionOrderDisplay::"+id+"::"+name+"::OnDisable()");
     }
     
     void OnDestroy()
     {
         Debug.Log(checkMode()+"::ExecutionOrderDisplay::"+id+"::"+name+"::OnDestroy()");
     }
 }

Crucially, note the [ExecuteInEditMode] directive, as it is what allows the script to function in both Edit and Play modes. To use the script, simply drag&drop it on an object's components list. In the case of a simple, default cube, it will result in something like this:

On Drag&Drop:

  • EditMode: Awake()

  • EditMode: OnEnable()

  • EditMode: Start()

On Switching to Play Mode:

  • EditMode: OnDisable()

  • EditMode: OnEnable()

  • EditMode: OnDisable()

  • EditMode: OnDestroy()

  • PlayMode: Awake()

  • PlayMode: OnEnable()

  • PlayMode: Start()

On Switching back to Edit Mode:

  • PlayMode: OnDisable()

  • PlayMode: OnDestroy()

  • EditMode: Awake()

  • EditMode: OnEnable()

  • EditMode: Start()

Somewhat interestingly, and maddeningly, is the OnEnable()/OnDisable pair after the first OnDisable() while switching from Edit Mode to Play mode. That's where most of the trouble occurs while trying to come up with custom serialization/file writing functionality. It's unclear why Unity must call that pair instead of simply OnDisable(), OnDestroy() and then entering Play mode. Perhaps is a recompilation step and re-enabling the script catches potential errors? Only speculation at this point as I am not aware of specific documentation on the matter.

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 ArkaneX · Sep 08, 2013 at 01:54 PM

Please check official documentation.

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 manu3d · Sep 08, 2013 at 02:11 PM 0
Share

I provide a link to the same page you suggest and it is not quite the end of the story. Specifically it doesn't address the behaviour of switching from Edit mode to Play mode and back.

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

16 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

Related Questions

Player damage stops working over time 0 Answers

Change play previw editor window size to match 1280x800 (android) 1 Answer

Is it possible to access the previous UnityPlayer.log file at runtime before it get's overwritten? 0 Answers

Bad Performance in Unity? 1 Answer

How do I detect that MonoBehaviour is modified in the Editor? 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