Description
The GetComponentsInParent
method retrieves components of a specified type from the current GameObject
and its ancestor GameObjects
. This method is useful when you need to access components that are not only on the current GameObject
but also on any of its parent objects in the hierarchy.
Usage
To use the GetComponentsInParent
method, specify the type of component you want to retrieve. You can also specify whether to include components from disabled GameObjects
and whether to include the component from the current GameObject
itself.
includeDisabled
: A bool
indicating whether to include components from disabled GameObjects
.
includeSelf
: A bool
indicating whether to include the component from the current GameObject
.
Example
// Example usage of GetComponentsInParent
public class ExampleComponent : Component
{
public void PrintParentComponents()
{
// Retrieve all components of type ExampleComponent in parent GameObjects
var components = GetComponentsInParent<ExampleComponent>(includeDisabled: true, includeSelf: true);
foreach (var component in components)
{
// Perform operations with each component
// For example, print the component's GameObject name
Log.Info(component.GameObject.Name);
}
}
}