Description
The GetComponentsInChildren
method retrieves all components of a specified 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 a GameObject
instance, specifying the type of components you want to retrieve. You can also specify whether to include components from disabled GameObject
s and whether to include the current GameObject
in the search.
Parameters:
includeDisabled
(bool
): If true
, components from disabled GameObject
s will be included in the result.
includeSelf
(bool
): If true
, components from the current GameObject
will be included in the result.
Example
// Example of using GetComponentsInChildren to retrieve all MeshRenderer components
GameObject myGameObject = new GameObject();
IEnumerable<MeshRenderer> meshRenderers = myGameObject.GetComponentsInChildren<MeshRenderer>(includeDisabled: true, includeSelf: true);
foreach (var renderer in meshRenderers)
{
// Perform operations with each MeshRenderer
}