- Home /
When do you use DontSave for Object's hideFlags?
What are the common use cases in which Object's hideFlags is set to DontSave?
Temporary objects, You might use them if you are writing an editor extension to help visualise things in the scene view (such as an object to show a waypoint) which you don't want to save as an actual object.
Scribe, can you answer below so I can accept your answer?
Answer by Scribe · Sep 01, 2012 at 08:55 PM
Reposted as an answer at the request of Superboonie88!
Temporary objects, You might use them if you are writing an editor extension to help visualise things in the scene view (such as an object to show a waypoint) which you don't want to save as an actual object.
Scribe
Yes, it's actually only used inside the editor. Since this is the only place where assets / data is serialized / saved. At runtime you can't "save" any assets or objects.
The best example is the camera for the SceneView which is attached to an invisible gameobject (HideAndDontSave). This gameobject is created when the sceneview is opened. Since it's a gameobject it is inside the current scene, but it isn't saved. It's also used for many other editor resources like textures for the GUI.
Answer by yoyo · Dec 03, 2015 at 12:55 AM
Almost never.
HideFlags.DontSave probably doesn't mean what you think it means. The documentation states:
The object will not be saved to the scene. It will not be destroyed when a new scene is loaded. It is a shortcut for HideFlags.DontSaveInBuild | HideFlags.DontSaveInEditor | HideFlags.DontUnloadUnusedAsset.
It is your responsibility to cleanup the object manually using DestroyImmediate, otherwise it will leak.
In other words, this should only be used when you are creating an object which you do not want saved, and you do not want it unloaded when the scene changes, and you are going to destroy the object manually yourself. (Note that Destroy is fine, you don't have to use DestroyImmediate.)
Your answer is a bit misleading. As Scribe said in his answer the hideflags are actually only relevant to editor program$$anonymous$$g as Unity doesn't (and actually can't) save anything at runtime / in a build game. Inside the editor you can't use Destroy as it's delayed to the end of the current frame. Inside the editor the runtime doesn't run so it can't be completed. If you use Destroy in the editor you will get an error and the object won't be destroyed. You have to use DestroyImmediate inside editor code.
While my answer might be slightly misleading, I$$anonymous$$HO it's not as misleading as the "DontSave" name of this flag. In particular, the "DontUnloadUnusedAsset" behaviour is quite surprising. I may have been feeling reactionary when I posted my answer, as today I discovered that our game is leaking objects because "DontSave" seemed like the right way to handle dynamically created objects. I stand by my answer and think Unity should remove the DontSave compound flag and require users to select the flags they actually need. Fair point about DestroyImmediate in editor, though the documentation should be much more clear about this.
I can't think of any usecase of "hideFlags" at runtime. As said the hideflags are for in editor use only. They make no sense at runtime. Even when "DontUnloadUnusedAsset" works at runtime, it makes no sense to use it. It will only affect objects which aren't referenced by any managed object anymore. So you would have to use FindObjectsOfType to regain access to those objects.
If an object might be used in the future, you want to keep a reference to it somehow. If you have a reference it won't be unloaded.
Just as reference the HideFlags documentation says:
Bit mask that controls object destruction, saving and visibility in inspectors.
The example code over here is complete nonsense. You'll never use hideflags at runtime since it's pointless.
I've written a hideflags editor window which can edit the hideflags of selected objects and also has a "hidden object explorer" at the bottom so you can select a hidden object as well and make it visible.
Your answer
Follow this Question
Related Questions
Changing a second objects material on trigger 0 Answers
Object disappear when position changes at runtime 0 Answers
Use more than a hideflag 1 Answer
Have a Timer Counts To 5 sec. Then Make a Sphere Appear While Using Instantiate 1 Answer
object can't be null when highlighted in the inspector? 1 Answer