menu_bookDocumentation
GameObjectFlags: DontDestroyOnLoad, NotNetworked, Absolute, and Destroy vs DestroyImmediate
GameObjectFlags: Controlling GameObject Behavior
GameObjectFlags is a flags enum that controls special behaviors for GameObjects.Values
| Flag | Effect | |---|---| | Hidden | Hide in hierarchy/inspector | | NotSaved | Don't save to disk or when duplicating | | Bone | Auto-created bone driven by animation | | Attachment | Auto-created attachment point | | DontDestroyOnLoad | Survives non-additive scene loads | | NotNetworked | Don't include in scene network snapshot | | EditorOnly | Only exists in editor, not spawned in game | | Absolute | Ignore parent transform (like position: absolute in CSS) | | PhysicsBone | Position controlled by physics (via Rigidbody) | | NoInterpolation | Disable interpolation from network and physics | | ProceduralBone | Stops animation from overriding the bone's local position |Usage
CSHARP
// Survive scene loads
myGameObject.Flags |= GameObjectFlags.DontDestroyOnLoad;
// Hide from inspector
myGameObject.Flags |= GameObjectFlags.Hidden;
// Don't network this object
myGameObject.Flags |= GameObjectFlags.NotNetworked;
// Check a flag
if ( myGameObject.Flags.Contains( GameObjectFlags.DontDestroyOnLoad ) )
{
// ...
}GameObject.Destroy() vs DestroyImmediate()
Destroy() queues the object for deletion at the start of the next frame (via Scene.QueueDelete). This is safe to call from anywhere. DestroyImmediate() destroys the object right now. This can cause issues if other code still holds a reference and expects the object to exist. Use with caution. After calling Destroy(), IsDestroyed returns true immediately (even before the actual deletion), so you can check it to avoid using a destroyed object.GameObject.Active vs Enabled
- Enabled — what this object wants to be
- Active — whether it actually is active (requires Enabled == true, all ancestors enabled, in a scene, and not network-culled)
Was this helpful?