- Home /
OnDestroy called when object lost focus in editor
Hi,
I don't understand how OnDestroy() and OnDisable() are supposed to work in custom editor scripts.
On the scene I have gameobject with script Manager
attached. Besides I have custom editor script ManagerEditor
:
[CustomEditor(typeof(Manager))]
[InitializeOnLoad]
public class ManagerEditor : Editor
{
public void OnDisable()
{
Debug.Log("Disable Editor");
}
public void OnDestroy()
{
Debug.Log("Destroy Editor");
}
}
Why, every time, I unselect this object (gameobject with Manager
attached) in hierarchy window, I see both Debug.Log messages (Disable Editor and Destroy Editor) ?
OnDestroy and OnDisable means losing focus ?
Thank you for clarifying this.
Answer by Sergio7888 · Sep 10, 2016 at 11:51 PM
When you deselect a object the editor is no longer used, is normal destroy the editor to release resource and make a new when you select the object again.
Yeah. After losing focus means destroying for custom editor.
Answer by lighting · Sep 11, 2016 at 10:17 AM
The solution is: place OnDestroy() in MonoBehavior script (instead of Editor script) and add ExecuteInEditor attribute for this MonoBehavior.
Wouldn't adding separate classes for the custom editor part and the GameObject/component part perhaps be better? Now things are mixed and indeed you get OnDisable() calls.
Your answer
Follow this Question
Related Questions
Detect when GameObject has been deleted / removed from scene 10 Answers
Need help reassigning variables after the deletion of an object has been undone. 0 Answers
OnDisable and OnDestroy Not Get Called - iOS 2 Answers
OnDisable/OnDestroy Execution Order 2 Answers
Does OnDisable()/OnStart() get always called OnApplicationClose? 0 Answers