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.

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.