Skip to content

format

typescript
GameObject.GetComponentInChildren(type)

class: GameObject

description

Get the first component of the specified type on the specified game object or any of its child objects.

This method first checks the game object it is called on, and then recursively traverses all child game objects using a depth-first search until it finds a matching component of the specified type.

parameter

param_nametypedescription
typeComponentThe type of the component.

reture

typedescription
T|nullThe component.

code example

typescript
let go = new GameObject();
// Get the BoxCollider component on the game object or any of its child objects
let box = go.GetComponentInChildren(BoxCollider);
if(box){
    Debug.Log(box);
}else{
    Debug.Log("Component not found");
}