GameObject.GetComponentCount
Declaration
public int GetComponentCount();Возвращаемое значение
инт Число компонентов на GameObject в виде целочисленного значения.
Описание
Получает общее число компонентов, в настоящее время подключенных к GameObject.
Вы можете использовать GetComponentCount для итерации по индексам компонентов, что особенно удобно, если использовать вместе с GameObject.GetComponentAtIndex. Например, вы можете итерировать по индексам, чтобы найти индекс конкретного интересующего вас компонента, а затем сохранить индекс для последующего использования.
using UnityEngine;
public class IterateComponents : MonoBehaviour { int m_SavedComponentIndex = -1;
void Start() { //Iterate through components on the GameObject for (int i = 0; i < gameObject.GetComponentCount(); i++) { var currComponent = gameObject.GetComponentAtIndex(i); //Check if it is a Rigidbody component if (currComponent.GetType() == typeof(Rigidbody) ) { m_SavedComponentIndex = i; } }
Debug.Log(m_SavedComponentIndex != -1 ? $"Found component at index: {m_SavedComponentIndex}" : "Could not find component"); } }
Дополнительные ресурсы: GameObject.GetComponentAtIndex