menu_bookDocumentation

FindMode and Component Lookup: GetComponent, GetAll, ComponentList, and Hierarchy Search

calendar_today May 4, 2026 schedule ~1 min read person patrickjr verified 50

FindMode and Component Lookup: GetComponent, GetAll, and ComponentList

s&box uses a FindMode flags enum to control how component searches traverse the hierarchy. Understanding this is essential for efficient component lookups.

FindMode Flags

CSHARP
[Flags]
public enum FindMode
{
    Enabled     = 1,   // Only enabled components
    Disabled    = 2,   // Only disabled components
    InSelf      = 4,   // Search this GameObject
    InParent    = 8,   // Search immediate parent
    InAncestors = 16,  // Search all ancestors
    InChildren  = 32,  // Search immediate children
    InDescendants = 64 // Search all descendants
}

Common Shorthand Modes

| Shorthand | Equivalent | |---|---| | EnabledInSelf | Enabled \| InSelf | | EverythingInSelf | Enabled \| Disabled \| InSelf | | EnabledInSelfAndDescendants | Enabled \| InSelf \| InDescendants | | EverythingInSelfAndDescendants | Enabled \| Disabled \| InSelf \| InDescendants | | EverythingInSelfAndAncestors | Enabled \| Disabled \| InSelf \| InAncestors |

Component Lookup Methods

CSHARP
// On a Component or GameObject:

// Get first enabled component of type T on this object
var rb = GetComponent<Rigidbody>();

// Get first component including disabled
var rb = GetComponent<Rigidbody>( includeDisabled: true );

// Get all components of type T on this object
var all = GetComponents<MyComponent>();

// Get first in hierarchy (self + descendants)
var child = GetComponentInChildren<MyComponent>();

// Get first in ancestors
var parent = GetComponentInParent<MyComponent>();

// Add a component
var rb = AddComponent<Rigidbody>();

// Get or add (won't duplicate)
var rb = GetOrAddComponent<Rigidbody>();

Direct ComponentList Access (More Control)

CSHARP
// Custom FindMode
var result = Components.Get<MyComponent>( FindMode.InAncestors | FindMode.Enabled );

// Get all with custom mode
var all = Components.GetAll<MyComponent>( FindMode.EverythingInSelfAndDescendants );

// Get or create
var rb = Components.GetOrCreate<Rigidbody>();

// Try get
if ( Components.TryGet<Rigidbody>( out var rb ) )
{
    // found
}

Performance Note

ComponentList is lazily initialized — GameObjects with no components don't allocate the internal list. The CollectAll hot path avoids LINQ for the common EnabledInSelfAndDescendants case.
Was this helpful?