GameObject.GetComponents

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

GameObject.GetComponents

Объявление

public Component[] GetComponents(Type type);

Параметры

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

Описание

Возвращает все компоненты типа type в GameObject.

Примечание. Если запрашиваемый тип является производным от MonoBehaviour и связанный скрипт не может быть загружен, тогда эта функция вернет `null` для этого компонента.

// Disable the spring on all HingeJoints in this game object
using UnityEngine;

public class GetComponentsExample : MonoBehaviour
{
    // Disable the spring on all HingeJoints in this game object

    void Start()
    {
        Component[] hingeJoints;

        hingeJoints = GetComponents(typeof(HingeJoint));

        foreach (HingeJoint joint in hingeJoints)
            joint.useSpring = false;
    }
}

Объявление

public T[] GetComponents();

Описание

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

Примечание. Если запрашиваемый тип является производным от MonoBehaviour и связанный скрипт не может быть загружен, тогда эта функция вернет `null` для этого компонента.

// Disable the spring on all HingeJoints in this game object
using UnityEngine;

public class GetComponentsExample : MonoBehaviour
{
    // Disable the spring on all HingeJoints in this game object


    void Start()
    {
        HingeJoint[] hingeJoints;

        hingeJoints = GetComponents<HingeJoint>();

        foreach (HingeJoint joint in hingeJoints)
            joint.useSpring = false;
    }
}

Объявление

public void GetComponents(Type type, List<Component> results);

Параметры

type Тип компонента для получения.
results Список для получения результатов.

Описание

Возвращает все компоненты типа type в GameObject в список results. Обратите внимание, что results имеет тип Component, а не тип извлеченного компонента.

Примечание. Если запрашиваемый тип является производным от MonoBehaviour и связанный скрипт не может быть загружен, тогда эта функция вернет `null` для этого компонента.

// Disable the spring on all HingeJoints in this game object
using UnityEngine;
using System.Collections.Generic;

public class GetComponentsExample : MonoBehaviour
{
    // Disable the spring on all HingeJoints in this game object

    void Start()
    {
        // Disable the spring on all HingeJoints in this game object
        List<Component> hingeJoints = new List<Component>();

        GetComponents(typeof(HingeJoint), hingeJoints);

        foreach (HingeJoint joint in hingeJoints)
            joint.useSpring = false;
    }
}

Объявление

public void GetComponents(List results);

Параметры

results List of type T to receive the results.

Описание

Возвращает все компоненты типа type в GameObject в список results.

Примечание. Если запрашиваемый тип является производным от MonoBehaviour и связанный скрипт не может быть загружен, тогда эта функция вернет `null` для этого компонента.

// Disable the spring on all HingeJoints in this game object
using UnityEngine;
using System.Collections.Generic;

public class GetComponentsExample : MonoBehaviour
{
    // Disable the spring on all HingeJoints in this game object


    void Start()
    {
        // Disable the spring on all HingeJoints in this game object
        List<HingeJoint> hingeJoints = new List<HingeJoint>();

        GetComponents(hingeJoints);

        foreach (HingeJoint joint in hingeJoints)
            joint.useSpring = false;
    }
}

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