格式
typescript
GameObject.GetComponentInParent(type)所属类: GameObject
描述
获取指定游戏对象或游戏对象的任何父级上的第一个该类型的组件。
此方法首先检查调用它的游戏对象,然后向上递归遍历每个父游戏对象,直到找到指定类型的匹配组件。
参数
| 参数名 | 类型 | 描述 |
|---|---|---|
| type | Component | 组件类型 |
返回值
| 类型 | 描述 |
|---|---|
T|null | 组件 |
代码示例
typescript
let go = new GameObject();
//获取游戏对象或游戏对象的任何父级上的BoxCollider组件
let box = go.GetComponentInParent(BoxCollider);
if(box){
Debug.Log(box);
}else{
Debug.Log("未找到组件");
}