GameObject.GetComponentInChildren

Unity · Версия 2020.3 LTS · Scripting · GameObject

GameObject.GetComponentInChildren

Объявление

public Component GetComponentInChildren(Type type);

public Component GetComponentInChildren(Type type, bool includeInactive);

Параметры

type Тип компонента для получения.

Возвращает

Component Компонент соответствующего типа, если он найден

Описание

Возвращает компонент Type type в GameObject или любом из его дочерних элементов, используя поиск в глубину.

Компонент возвращается, только если он найден в активном игровом объекте.

using UnityEngine;

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

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

        if (hinge != null)
            hinge.useSpring = false;
        else
        {
            // Try again, looking for inactive GameObjects
            HingeJoint hingeInactive = gameObject.GetComponentInChildren(typeof(HingeJoint), true) as HingeJoint;

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

Объявление

public T GetComponentInChildren(bool includeInactive = false);

Возвращает

T Компонент соответствующего типа, если он найден.

Описание

Общая версия этого метода.

using UnityEngine;

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

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

        if (hinge != null)
            hinge.useSpring = false;
        else
        {
            // Try again, looking for inactive GameObjects
            HingeJoint hingeInactive = gameObject.GetComponentInChildren<HingeJoint>(true) as HingeJoint;

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

Обновлено 17 ноября 2026 г.