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
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 this camera a simple component to track the gameobject state changed:

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


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 something 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). This 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 thing when you stop the game. The object gets recreated after you're back in editmode and Unity deserializes everything 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 ExecuteInEdit$$anonymous$$ode? - 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 ExecuteInEdit$$anonymous$$ode 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 ExecuteInEdit$$anonymous$$ode you should always make sure you distinguish between editmode and playmode. Application.isPlaying can be used to deter$$anonymous$$e if you are in playmode.

Also keep in $$anonymous$$d 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.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

OnEnable called before Awake 2 Answers

OnEnable and Awake depreciated? 1 Answer

Am I using OnEnable() correctly here? 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