Description
The GetComponentsInParent
method retrieves all components of type T
from the current GameObject
and its ancestor GameObjects
. This method can include components that are disabled and can also include the component on the current GameObject
itself, based on the parameters provided.
Usage
To use the GetComponentsInParent
method, specify the type of component you want to retrieve. You can control whether to include disabled components and whether to include the component on the current GameObject
itself by setting the includeDisabled
and includeSelf
parameters respectively.
Parameters:
includeDisabled
(bool
): If set to true
, the method will include components that are currently disabled.
includeSelf
(bool
): If set to true
, the method will include the component on the current GameObject
itself.
Example
// Example of using GetComponentsInParent
public void ExampleUsage()
{
// Assume this is a method within a Component-derived class
var components = GetComponentsInParent<MyComponentType>(includeDisabled: true, includeSelf: true);
foreach (var component in components)
{
// Perform operations with each component
component.DoSomething();
}
}