Hi.
I have a GameObject with a simple script in C#, which has [ExecuteInEditMode] , however, when OnEnable runs, I want to be able to diferentiate if I’m in Play mode, or in the Editor. For which I am using Application.IsPlaying . However, when I click on Play, the OnEnable triggers twice, one with Application.IsPlaying being set to true, and another with it being set to false, this leads to some weird behavior. Is this expected? I don’t remember having this problem previously.
What would be the architecture guidelines to have one single call on each, at the appropriate times (Editor OnEnable, Play OnEnable).
Here is the script:
#if UNITY_EDITOR
using UnityEditor;
#endif
[ExecuteInEditMode]
public class TestScript : MonoBehaviour {
void OnEnable(){
Debug.Log("On Enable");
if (Application.isPlaying)
PlayOnEnable();
else
EditorOnEnable();
}
void PlayOnEnable(){
Debug.Log("Play On Enable.");
}
void EditorOnEnable(){
Debug.Log("Editor On Enable.");
}
}
When I enter Play mode in the editor, OnEnable is triggered twice, one for EditorOnEnable and another for PlayOnEnable, in this order.
Thank you in advance