Description
The GetComponentsInParent
method retrieves all components of a specified type T
from the current GameObject
and its ancestor GameObjects
. This method can include components from disabled GameObjects
and can also include the components from the current GameObject
itself, based on the parameters provided.
Usage
To use the GetComponentsInParent
method, specify the type of component you want to retrieve using the generic type parameter T
. You can control whether to include components from disabled GameObjects
and whether to include components from the current GameObject
by setting the includeDisabled
and includeSelf
parameters, respectively.
Parameters:
includeDisabled
(bool
): If set to true
, components from disabled GameObjects
will be included in the result.
includeSelf
(bool
): If set to true
, components from the current GameObject
will be included in the result.
Example
// Example usage of GetComponentsInParent
public void ExampleUsage(GameObject gameObject)
{
// Retrieve all components of type MyComponent from the current GameObject and its parents
// Include components from disabled GameObjects and include components from the current GameObject
var components = gameObject.GetComponentsInParent<MyComponent>(includeDisabled: true, includeSelf: true);
foreach (var component in components)
{
// Process each component
component.DoSomething();
}
}