- Home /
 
How to manage temporary GameObject with HideFlags.DontSave on recompile
I have a custom editor for a scene component which draws temporary preview objects at edit time. Those objects have HideFlags.DontSave set, so that they are deleted when the scene is closed. (Is this bad practice, should I be checking for ApplicationQuit and delete them myself?)
My editor code uses OnEnable to find an existing temp object or create a new one. Everything works fine until I recompile, at which point I get multiple copies of the temp objects in the scene. Any ideas how to avoid this? It's not a big problem to delete additional copies as soon as I find them, but it feels dirty. There must be a clean way, just like the SceneView camera object works. Can I maybe use callbacks to store references or delete them at recompilation?
 [CustomEditor(typeof(MapView))]
 public class MapEditor : Editor
 {
     PreviewObject previewObject;
 
     void OnEnable()
     {
         var allPreviews = Resources.FindObjectsOfTypeAll<PreviewObject>();
         if (allPreviews.Length > 0)
             previewObject = allPreviews[0];
 
         if (previewObject == null)
         {
             var go = EditorUtility.CreateGameObjectWithHideFlags("Preview Object", HideFlags.DontSave, typeof(PreviewObject));
             previewObject = go.GetComponent<PreviewObject>();
         }
     }
 }
 
              Your answer
 
             Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
instantiate gameobject in editor 1 Answer
HideInHierarchy not hiding root objects 0 Answers
Setting variables in public class to values from editor script 3 Answers