IEnumerable<T> GetComponentsInParent( bool includeDisabled, bool includeSelf )

book_4_sparkGenerated
code_blocksInput

Description

The GetComponentsInParent method retrieves all components of a specified type T from the current GameObject and its ancestor GameObjects. This method is useful when you need to access components that might be located on parent objects in the hierarchy.

Usage

To use the GetComponentsInParent method, specify the type of component you are looking for as the generic type parameter T. You can also specify whether to include components from disabled GameObjects and whether to include the component on the current GameObject itself.

Parameters:

  • includeDisabled (Boolean): If set to true, the method will include components from disabled GameObjects.
  • includeSelf (Boolean): If set to true, the method will include the component on the current GameObject if it exists.

Example

// Example usage of GetComponentsInParent
public void ExampleMethod(Component component)
{
    // Get all components of type MyComponent in the parent hierarchy
    var components = component.GetComponentsInParent<MyComponent>(includeDisabled: true, includeSelf: true);
    
    foreach (var comp in components)
    {
        // Perform operations with each component
        comp.DoSomething();
    }
}