# GetComponentAtIndex

> Retrieves a reference to a component at a specified index of the GameObject's array of components.

## Definition

* **Type:** Method
* **Namespace:** [UnityEngine](/engine/6000.0/script-reference/unityengine.md)
* **Assembly:** UnityEngine.CoreModule

## GetComponentAtIndex(int)

Retrieves a reference to a component at a specified index of the GameObject's array of components.

```csharp
public Component GetComponentAtIndex(int index)
```

### Parameters

**** (\[int]\(https\://learn.microsoft.com/dotnet/api/system.int32)): The index position in the array of components at which to find the requested object.

### Returns

| Type                                                                  | Description                                                                                                         |
| --------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------- |
| [Component](/engine/6000.0/script-reference/unityengine/component.md) | A reference to a component at the specified index. If no component is found at the specified index, returns `null`. |

### Remarks

Using `GetComponentAtIndex` is a stable way to access components on a GameObject because the index of a component stays the same unless components are added or removed.

An example use case for this is during UI development. This method throws an exception if the index is out of bounds. Refer to [GameObject.GetComponentCount](/engine/6000.0/script-reference/unityengine/gameobject/getcomponentcount.md) for more information.

```csharp
using UnityEngine;

public class GetComponentAtIndexExample : MonoBehaviour
{
    public GameObject otherGameObject;

    void Start()
    {
        HingeJoint hinge = otherGameObject.GetComponentAtIndex(5) as HingeJoint;

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

Additional Resources: [Component](/engine/6000.0/script-reference/unityengine/component.md), [GameObject.GetComponents](/engine/6000.0/script-reference/unityengine/gameobject/getcomponents.md)

## GetComponentAtIndex\<T>(int)

Retrieves a reference to a component of type T at a specific index on the specified GameObject.

```csharp
public T GetComponentAtIndex<T>(int index) where T : Component
```

### Parameters

**** (\[int]\(https\://learn.microsoft.com/dotnet/api/system.int32)): The index position in the array of components at which to find the requested object.

### Returns

| Type | Description                                                                                                                     |
| ---- | ------------------------------------------------------------------------------------------------------------------------------- |
| T    | A reference to a component of type `T` at the specified index. If no component is found at the specified index, returns `null`. |

### Remarks

Using `GetComponentAtIndex` is a stable way to access components on a GameObject because the index of a component stays the same unless components are added or removed.

An example use case for this is during UI development. This method throws an exception if the index is out of bounds.

Additional Resources: [GameObject.GetComponentCount](/engine/6000.0/script-reference/unityengine/gameobject/getcomponentcount.md)

```csharp
using UnityEngine;

public class GetTComponentAtIndexExample : MonoBehaviour
{
    public GameObject otherGameObject;

    void Start()
    {
        HingeJoint hinge = otherGameObject.GetComponentAtIndex<HingeJoint>(5);

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

Additional Resources: [Component](/engine/6000.0/script-reference/unityengine/component.md), [GameObject.GetComponents](/engine/6000.0/script-reference/unityengine/gameobject/getcomponents.md)
