# AddComponent

> Adds a component of the specified type to the GameObject.

## Definition

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

## AddComponent(Type)

Adds a component of the specified type to the GameObject.

```csharp
public Component AddComponent(Type componentType)
```

### Parameters

**** (\[Type]\(https\://learn.microsoft.com/dotnet/api/system.type)):&#x20;

### Returns

| Type                                                                  | Description |
| --------------------------------------------------------------------- | ----------- |
| [Component](/engine/6000.0/script-reference/unityengine/component.md) |             |

### Remarks

There is no corresponding method for removing a component from a GameObject. To remove a component, use [Object.Destroy](/engine/6000.0/script-reference/unityengine/object/destroy.md).

```csharp
using UnityEngine;
using System.Collections;

public class AddComponentExample : MonoBehaviour
{
    void Start()
    {
        SphereCollider sc = gameObject.AddComponent(typeof(SphereCollider)) as SphereCollider;
    }
}
```

Additional Resources: [Component](/engine/6000.0/script-reference/unityengine/component.md), [Object.Destroy](/engine/6000.0/script-reference/unityengine/object/destroy.md)

## AddComponent\<T>()

Generic version of this method.

```csharp
public T AddComponent<T>() where T : Component
```

### Returns

| Type | Description |
| ---- | ----------- |
| T    |             |

### Remarks

```csharp
using UnityEngine;
using System.Collections;

public class AddComponentExample : MonoBehaviour
{
    void Start()
    {
        SphereCollider sc = gameObject.AddComponent<SphereCollider>();
    }
}
```

Additional Resources: [Component](/engine/6000.0/script-reference/unityengine/component.md), [Object.Destroy](/engine/6000.0/script-reference/unityengine/object/destroy.md)

## AddComponent(string)

Adds a component of the specified class name to the GameObject.

> **Warning:**
>
> **Removed.** GameObject.AddComponent with string argument has been deprecated. Use GameObject.AddComponent\\\<T\\>() instead..

```csharp
public Component AddComponent(string className)
```

### Parameters

**** (\[string]\(https\://learn.microsoft.com/dotnet/api/system.string)):&#x20;

### Returns

| Type                                                                  | Description |
| --------------------------------------------------------------------- | ----------- |
| [Component](/engine/6000.0/script-reference/unityengine/component.md) |             |

### Remarks

Deprecated: Use AddComponent(Type) or the generic version of this method instead.
