Description
The GetInAncestorsOrSelf
method in the ComponentList
class is used to find a component of type T
on the current game object or any of its ancestor objects. This method is useful when you need to search for a component not only on the current object but also in its hierarchy, moving upwards through its parent objects.
Usage
To use the GetInAncestorsOrSelf
method, call it on an instance of ComponentList
. You need to specify whether to include disabled components in the search by passing a boolean value to the includeDisabled
parameter.
Parameters:
includeDisabled
(Boolean): If set to true
, the method will include disabled components in the search. If false
, only enabled components will be considered.
Returns: The method returns a component of type T
if found; otherwise, it returns null
.
Example
// Example usage of GetInAncestorsOrSelf
ComponentList componentList = gameObject.GetComponentList();
MyComponentType component = componentList.GetInAncestorsOrSelf<MyComponentType>(includeDisabled: true);
if (component != null)
{
// Component found, perform operations on it
}
else
{
// Component not found
}