Skip to content

format

typescript
GameObject.GetComponentInParent(type)

class: GameObject

description

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

This method first checks the game object it is called on, and then recursively traverses up each parent game object 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 specified game object or any of its parent game objects
let box = go.GetComponentInParent(BoxCollider);
if(box){
    Debug.Log(box);
}else{
    Debug.Log("Component not found");
}