Understanding of HideAndDontSave hide flag.

Hi!

I found this explanation:
"HideFlags
In the examples using ScriptableObjects you will notice that we are setting the ‘hideFlags’ on the object to HideFlags.HideAndDontSave. This is a special setup that is required when writing custom data structures that have no root in the scene. This is to get around how scene loading works in Unity.

When a scene is loaded internally unity calls Resources.UnloadUnusedAssets. If nothing is referencing an asset the garbage collector will find it. The GC uses the scene as ‘the root’ and traverses the hierarchy to see what can get GC’d. Setting the HideAndDontSave flag on a ScriptableObject tells Unity to consider that object as a root object. Because of this it will not just disappear because of an assembly reload. The object can still be destroyed by calling Destroy()."

What I can’t understand is this part:
“Setting the HideAndDontSave flag on a ScriptableObject tells Unity to consider that object as a root object”
Root ? What root ? Where that root is ?

I’ve created some image as my guess:

Is that what they mean ?

Thank you in advance.

1 Like

Its a GC Root. https://www.simple-talk.com/dotnet/.net-framework/understanding-garbage-collection-in-.net/

Thank you! I’ll take a look.

Sorry for the late answer, probably my notification settings on the forum is not set.

1 Like

I’m still confused by this. In

If nothing is referencing an asset the garbage collector will find it.

What could be referencing an asset? Aren’t scriptable objects always referenced somewhere? Otherwise why wouldn’t you want them to be deleted?

Because you may want to destroy the asset / game object which is referencing this object. And you don’t want it to disappear (for example contains a bunch of runtime generated data and unloading/reloading would mean you need to rerun the lengthy calculation).
This is why the HideAndDontSave is a special case. It allows “loose” game objects and assets existing without destroying them. Normally you would want them to be unloaded.

But if the objects are loose and inaccessible wouldn’t that mean you’d never want them not to be unloaded?