- Home /
ExecuteInEditMode issue
I have a script with [ExecuteInEditMode] and Update is only called when the gameobject is moved etc, i would like to do something when the object is selected but it is not being called because of this, is there a way to have Update run all the time?
Answer by FM-Productions · Jun 01, 2017 at 01:17 PM
As you can see in https://docs.unity3d.com/ScriptReference/ExecuteInEditMode.html the update is only called when something in the scene changes, for example panning around the scene. From the article:
"OnGUI is called when the Game View recieves an Event. "
Maybe you could do something with that information and try to implement it in the OnGUI method. But is it really necessary to execute updates in regular intervals?
Use this to check your selected gameObject:
https://docs.unity3d.com/ScriptReference/Selection-activeGameObject.html
if Selection.activeGameObject
is your desired gameObject in the current update, but was not your desired gameObject in the previous frame, apply your logic.
I'm already using Selection.Contains(gameObject) and OnGui isn't helping, i really want an execution in my code every time the gameobject is selected, i don't need the Update every frames but at least once when the selection happens so i'm trying to find a solution.
Right now i'm thinking about adding a menu option on right click that would execute my function.
$$anonymous$$aybe you could abuse the OnDrawGizmosSelected function, but I don't know if that works.
https://docs.unity3d.com/ScriptReference/$$anonymous$$onoBehaviour.OnDrawGizmosSelected.html In the function, check if you are in the Editor, then apply your logic:
if (Application.isEditor) {
//your logic
}
OnDrawGizmosSelected() Worked perfectly, not sure why i haven't found that workaround anywhere, thanks!