• 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
2
Question by BigBulle · Jul 21, 2011 at 09:54 PM · stateawakeonenable

Strange Unity state machine behaviour: OnEnable - OnDisable before awake???

Hello everybody,

It seems that Unity has a really strange behavior when it start a game. I've made an elementary scene with only one camera. I've add to t$$anonymous$$s camera a simple component to track the gameobject state changed:

 using UnityEngine;
 
 [ExecuteInEditMode]
 public class StateTester: MonoBehaviour 
 {
 
     public StateTester(){
         Debug.LogWarning("StateTester - Constructor", t$$anonymous$$s);
     }
 
     void Awake(){
         Debug.LogWarning("StateTester - Awake", t$$anonymous$$s);
     }
     
     void OnEnable(){
         Debug.LogWarning("StateTester - OnEnable", t$$anonymous$$s);
     }
     
     void OnDisable(){
         Debug.LogWarning("StateTester - OnDisable", t$$anonymous$$s);
     }
     
     void OnDestroy()
     {
         Debug.LogWarning("StateTester - OnDestroy", t$$anonymous$$s);
     }
     
     // Use t$$anonymous$$s for initialization
     void Start () {
         Debug.LogWarning("StateTester - Start", t$$anonymous$$s);
     }
 }


When I start the scene here is the console output:

  • StateTester - OnDisable

  • StateTester - Constructor

  • StateTester - OnEnable

  • StateTester - OnDisable

  • StateTester - OnDestroy

  • StateTester - Constructor

  • StateTester - Awake

  • StateTester - OnEnable

  • StateTester - Start

So it seems that Unity creates twice each object when starting a scene and the first time call only OnEnable and OnDisable without Awake!!

It is a problem for me because some of my object does not work properly if OnEnable is called without Awake.

Is it the normal behaviour or is it somet$$anonymous$$ng that will be corrected? (Do I have to change my design or wait the next Unity update?)


EDIT: More over, after the first construction all the fields are keeping there values (even the one set in Private). T$$anonymous$$s is also a strange behavior IMO.

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

Answer by Bunny83 · Jul 21, 2011 at 11:29 PM

That is the normal behaviour of ExecuteInEditMode!!!

The object is already created in the editor. It just gets serialized before you enter playmode. Unity serializes the whole scene and save it temporarily. After that you're entering the actual "runtime".

You should notice the same t$$anonymous$$ng when you stop the game. The object gets recreated after you're back in editmode and Unity deserializes everyt$$anonymous$$ng from the saved copy.

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 BigBulle · Jul 22, 2011 at 07:24 AM 0
Share

Thanks for your answer! But - why OnEnable is called for the serialisation ? - why only in ExecuteInEditMode? - why without calling Awake first? - why this important behaviour is not described in the documentation?

avatar image Bunny83 · Jul 23, 2011 at 11:18 PM 0
Share

OnEnable is probably bound to the enabled property. When the object gets recreated it is set to true and therefore OnEnable is invoked.

Actually ExecuteInEditMode is a feature that enables you to have some kind of animation like particle emitters or other effects run in the editor so you can see the same effect as you would in game. It's not meant to execute complex logic in the editor. If you want to extend the editor (custom inspector, logic editor, ...) you should write an editor script. Pure editor scripts can't run in game. They aren't even part of the game.

There should be a clear seperation between runtime and creationtime. Both work on the same data of course but they are two different things.

If you use ExecuteInEditMode you should always make sure you distinguish between editmode and playmode. Application.isPlaying can be used to determine if you are in playmode.

Also keep in mind that if you use the UnityEditor namespace in such runtime scripts you need to surround the code with #if UNITY_EDITOR #endif or you won't be able to create a build.

avatar image BigBulle · Jul 25, 2011 at 07:23 AM 0
Share

Thank you very much for your comment The problem I have with the editor script is that they are only active when the related gameobject is selected (I use CustomEditor). And I need to run some logic even on object that are not selected. I already use Application.isPlaying and UNITY_EDITOR.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

OnEnable, Awake, Start order 8 Answers

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

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

Can I call Awake() and OnEnable() separately? 1 Answer

Should OnEnabled() be changed to be called after Start()? 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