Description
The GetComponentsInChildren<T>
method retrieves all components of type T
from the current GameObject
and its descendant GameObject
s. This method allows you to specify whether to include components from disabled GameObject
s and whether to include the components from the current GameObject
itself.
Usage
To use this method, call it on an instance of a GameObject
. You can specify the type of components you want to retrieve by using a generic type parameter T
. The method takes two boolean parameters:
includeDisabled
: If set to true
, the method will include components from disabled GameObject
s.
includeSelf
: If set to true
, the method will include components from the current GameObject
itself.
Example
// Example usage of GetComponentsInChildren<T>
GameObject myGameObject = new GameObject();
// Retrieve all components of type MyComponent from myGameObject and its children
var components = myGameObject.GetComponentsInChildren<MyComponent>(includeDisabled: true, includeSelf: true);
foreach (var component in components)
{
// Perform operations with each component
component.DoSomething();
}