# GetComponentCount()

> Retrieves the total number of components currently attached to the GameObject.

## Definition

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

```csharp
public int GetComponentCount()
```

### Returns

| Type                                                       | Description                                                     |
| ---------------------------------------------------------- | --------------------------------------------------------------- |
| [int](https://learn.microsoft.com/dotnet/api/system.int32) | The number of components on the GameObject as an Integer value. |

### Remarks

You can use `GetComponentCount` to iterate through component indices, which is especially convenient if used together with [GameObject.GetComponentAtIndex](/engine/6000.5/script-reference/unityengine/gameobject/getcomponentatindex.md). For example, you can iterate through the indices to find the index of a particular component you're interested in and then save the index for later use.

```csharp
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");
    }
}
```

Additional Resources: [GameObject.GetComponentAtIndex](/engine/6000.5/script-reference/unityengine/gameobject/getcomponentatindex.md)
