Description
The GetComponentsInParent<T>
method retrieves all components of type T
from the current GameObject
and its ancestor GameObjects
. This method allows you to specify whether to include components from disabled GameObjects
and whether to include the components from the current GameObject
itself.
Usage
To use the GetComponentsInParent<T>
method, call it on a GameObject
instance, specifying the type of component you want to retrieve. You can also specify whether to include components from disabled GameObjects
and whether to include the current GameObject
in the search.
Parameters:
includeDisabled
(bool
): If true
, components from disabled GameObjects
will be included in the results.
includeSelf
(bool
): If true
, components from the current GameObject
will be included in the results.
Example
// Example of using GetComponentsInParent<T> to retrieve all Rigidbody components
// from the current GameObject and its ancestors, including disabled GameObjects
// and the current GameObject itself.
var rigidbodies = myGameObject.GetComponentsInParent<Rigidbody>(includeDisabled: true, includeSelf: true);
foreach (var rb in rigidbodies)
{
// Perform operations with each Rigidbody component
rb.AddForce(Vector3.up * 10);
}