# AddComponent

> Creates a new component and adds it to the specified GameObject.

## Definition

* **Type:** Method
* **Namespace:** [UnityEditor](/engine/6000.6/script-reference/unityeditor.md)
* **Assembly:** UnityEditor.CoreModule

## AddComponent\<T>(GameObject)

Creates a new component and adds it to the specified GameObject.

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

### Parameters

**** (\[GameObject]\(/engine/6000.6/script-reference/unityengine/gameobject)): The GameObject to add the new component to.

### Returns

| Type | Description                                                         |
| ---- | ------------------------------------------------------------------- |
| T    | Returns the component that was created and added to the GameObject. |

### Examples

```csharp
using UnityEngine;
using UnityEditor;

public class CreateComponentExample
{
    [MenuItem("ObjectFactoryExample/Add Camera to Selection")]
    public void AddDefaultComponentEditor()
    {
        if (Selection.activeGameObject != null)
        {
            Camera camera = ObjectFactory.AddComponent<Camera>(Selection.activeGameObject);
        }
    }
}
```

## AddComponent(GameObject, Type)

Creates a new component and adds it to the specified GameObject.

```csharp
public static Component AddComponent(GameObject gameObject, Type type)
```

### Parameters

**** (\[GameObject]\(/engine/6000.6/script-reference/unityengine/gameobject)): The GameObject to add the new component to.**** (\[Type]\(https\://learn.microsoft.com/dotnet/api/system.type)): The type of component to create and add to the GameObject.

### Returns

| Type                                                                  | Description                                                         |
| --------------------------------------------------------------------- | ------------------------------------------------------------------- |
| [Component](/engine/6000.6/script-reference/unityengine/component.md) | Returns the component that was created and added to the GameObject. |

### Examples

```csharp
using UnityEngine;
using UnityEditor;

public class CreateComponentExample
{
    [MenuItem("ObjectFactoryExample/Add Camera to Selection")]
    public void AddDefaultComponentEditor()
    {
        if (Selection.activeGameObject != null)
        {
            Camera camera = ObjectFactory.AddComponent<Camera>(Selection.activeGameObject);
        }
    }
}
```
