Search code examples
unity-game-engineunity3d-gui

Unity3D: void OnEnable() is called Unpredictably in Editor Mode


I implemented OnEnable() and it is always called in Play mode as expected. But in Editor Mode some components call OnEnable() and other don't

I need OnEnable() or other method to be called in Editor Mode to setup component to be able to prototype it.

If I extend class from UnityEngine.UI.Image then OnEnable() is called in Editor Mode. If I extend class from MonoBehavior then it does not call OnEnable() in Editor Mode but still being called in Play Mode.

Is there any clarification why it is called or not called.


Solution

  • MonoBehaviour.OnEnable() is called when the script component is actived. UnityEngine.UI.Image inherits from MonoBehaviour, so there should be no difference. You may need to have a look at Execution Order of Event Functions.

    If you want your MonoBehaviour called by the editor, you should add a ExecuteInEditMode attribute to your class.