GameObject.GetComponentInParent

切换到手册
public Component GetComponentInParent (Type type);

参数

type要查找的组件的类型。

返回

Component 如果发现与类型匹配的组件,则返回组件。否则返回 null。

描述

获取 GameObject 或其任何父项中 Type type 的组件。

此方法向上递归,直到其找到具有匹配组件的 GameObject 为止。仅匹配活动 GameObjects 上的组件。

using UnityEngine;
using System.Collections;

public class GetComponentInParentExample : MonoBehaviour { // Disable the spring on the first HingeJoint component found on any parent object

void Start() { HingeJoint hinge = gameObject.GetComponentInParent(typeof(HingeJoint)) as HingeJoint;

if (hinge != null) hinge.useSpring = false; } }

public T GetComponentInParent ();

返回

T 如果发现与类型匹配的组件,则返回组件。否则返回 null。

描述

通用版本。有关更多详细信息,请参阅通用函数页面。

using UnityEngine;
using System.Collections;

public class GetComponentInParentExample : MonoBehaviour { // Disable the spring on the first HingeJoint component found on any parent object

void Start() { HingeJoint hinge = gameObject.GetComponentInParent<HingeJoint>();

if (hinge != null) hinge.useSpring = false; } }
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961