# AddComponent

> Records a command to add component of type T to an entity.

## Definition

* **Type:** Method
* **Namespace:** [Unity.Entities](/engine/7000.0/script-reference/unity/entities.md)
* **Assembly:** UnityEngine.EntitiesModule

## AddComponent\<T>(int, Entity, T)

Records a command to add component of type T to an entity.

```csharp
public void AddComponent<T>(int sortKey, Entity e, T component) where T : unmanaged, IComponentData
```

### Parameters

**** (\[int]\(https\://learn.microsoft.com/dotnet/api/system.int32)): A unique index for each set of commands added to this EntityCommandBuffer across all parallel jobs writing commands to this buffer. The [ChunkIndexInQuery](/engine/7000.0/script-reference/unity/entities/chunkindexinquery.md) provided by [IJobEntity](/engine/7000.0/script-reference/unity/entities/ijobentity.md) is an appropriate value to use for this parameter. In an [IJobChunk](/engine/7000.0/script-reference/unity/entities/ijobchunk.md) pass the 'unfilteredChunkIndex' value from IJobChunk.Execute.**** (\[Entity]\(/engine/7000.0/script-reference/unity/entities/entity)): The entity to have the component added.**** (T): The value to add on the new component in playback for the entity.

### Remarks

At playback, if the entity already has this type of component, the value will just be set. Will throw an error if this entity is destroyed before playback, if T is type Entity or [Prefab](/engine/7000.0/script-reference/unity/entities/prefab.md), or adding this componentType makes the archetype too large.

## AddComponent\<T>(int, NativeArray\<Entity>, T)

Records a command to add component of type T to a NativeArray of entities.

```csharp
public void AddComponent<T>(int sortKey, NativeArray<Entity> entities, T component) where T : unmanaged, IComponentData
```

### Parameters

**** (\[int]\(https\://learn.microsoft.com/dotnet/api/system.int32)): A unique index for each set of commands added to this EntityCommandBuffer across all parallel jobs writing commands to this buffer. The [ChunkIndexInQuery](/engine/7000.0/script-reference/unity/entities/chunkindexinquery.md) provided by [IJobEntity](/engine/7000.0/script-reference/unity/entities/ijobentity.md) is an appropriate value to use for this parameter. In an [IJobChunk](/engine/7000.0/script-reference/unity/entities/ijobchunk.md) pass the 'unfilteredChunkIndex' value from IJobChunk.Execute.**** (\[NativeArray\<Entity>]\(/engine/7000.0/script-reference/unity/collections/nativearray1)): The NativeArray of entities to have the component added.**** (T): The value to add on the new component in playback for all entities in the NativeArray.

### Remarks

At playback, if any entity already has this type of component, the value will just be set. Will throw an error if any entity is destroyed before playback, if T is type Entity or [Prefab](/engine/7000.0/script-reference/unity/entities/prefab.md), or adding this componentType makes the archetype too large.

## AddComponent\<T>(int, Entity)

Records a command to add component of type T to an entity.

```csharp
public void AddComponent<T>(int sortKey, Entity e) where T : unmanaged, IComponentData
```

### Parameters

**** (\[int]\(https\://learn.microsoft.com/dotnet/api/system.int32)): A unique index for each set of commands added to this EntityCommandBuffer across all parallel jobs writing commands to this buffer. The [ChunkIndexInQuery](/engine/7000.0/script-reference/unity/entities/chunkindexinquery.md) provided by [IJobEntity](/engine/7000.0/script-reference/unity/entities/ijobentity.md) is an appropriate value to use for this parameter. In an [IJobChunk](/engine/7000.0/script-reference/unity/entities/ijobchunk.md) pass the 'unfilteredChunkIndex' value from IJobChunk.Execute.**** (\[Entity]\(/engine/7000.0/script-reference/unity/entities/entity)): The entity to have the component added.

### Remarks

At playback, this command will do nothing if the entity already has the component. Will throw an error if this entity is destroyed before playback, if T is type Entity or [Prefab](/engine/7000.0/script-reference/unity/entities/prefab.md), or adding this componentType makes the archetype too large.

## AddComponent\<T>(int, NativeArray\<Entity>)

Records a command to add component of type T to a NativeArray of entities.

```csharp
public void AddComponent<T>(int sortKey, NativeArray<Entity> entities) where T : unmanaged, IComponentData
```

### Parameters

**** (\[int]\(https\://learn.microsoft.com/dotnet/api/system.int32)): A unique index for each set of commands added to this EntityCommandBuffer across all parallel jobs writing commands to this buffer. The [ChunkIndexInQuery](/engine/7000.0/script-reference/unity/entities/chunkindexinquery.md) provided by [IJobEntity](/engine/7000.0/script-reference/unity/entities/ijobentity.md) is an appropriate value to use for this parameter. In an [IJobChunk](/engine/7000.0/script-reference/unity/entities/ijobchunk.md) pass the 'unfilteredChunkIndex' value from IJobChunk.Execute.**** (\[NativeArray\<Entity>]\(/engine/7000.0/script-reference/unity/collections/nativearray1)): The NativeArray of entities to have the component added.

### Remarks

At playback, if an entity already has this component, it will be skipped. Will throw an error if any entity is destroyed before playback, if T is type Entity or [Prefab](/engine/7000.0/script-reference/unity/entities/prefab.md), or adding this componentType makes the archetype too large.

## AddComponent(int, Entity, ComponentType)

Records a command to add a component to an entity.

```csharp
public void AddComponent(int sortKey, Entity e, ComponentType componentType)
```

### Parameters

**** (\[int]\(https\://learn.microsoft.com/dotnet/api/system.int32)): A unique index for each set of commands added to this EntityCommandBuffer across all parallel jobs writing commands to this buffer. The [ChunkIndexInQuery](/engine/7000.0/script-reference/unity/entities/chunkindexinquery.md) provided by [IJobEntity](/engine/7000.0/script-reference/unity/entities/ijobentity.md) is an appropriate value to use for this parameter. In an [IJobChunk](/engine/7000.0/script-reference/unity/entities/ijobchunk.md) pass the 'unfilteredChunkIndex' value from IJobChunk.Execute.**** (\[Entity]\(/engine/7000.0/script-reference/unity/entities/entity)): The entity to get the additional component.**** (\[ComponentType]\(/engine/7000.0/script-reference/unity/entities/componenttype)): The type of component to add.

### Remarks

At playback, this command will do nothing if the entity already has the component. Will throw an error if this entity is destroyed before playback, if component type is type Entity or [Prefab](/engine/7000.0/script-reference/unity/entities/prefab.md), or adding this componentType makes the archetype too large.

## AddComponent(int, NativeArray\<Entity>, ComponentType)

Records a command to add a component to a NativeArray of entities.

```csharp
public void AddComponent(int sortKey, NativeArray<Entity> entities, ComponentType componentType)
```

### Parameters

**** (\[int]\(https\://learn.microsoft.com/dotnet/api/system.int32)): A unique index for each set of commands added to this EntityCommandBuffer across all parallel jobs writing commands to this buffer. The [ChunkIndexInQuery](/engine/7000.0/script-reference/unity/entities/chunkindexinquery.md) provided by [IJobEntity](/engine/7000.0/script-reference/unity/entities/ijobentity.md) is an appropriate value to use for this parameter. In an [IJobChunk](/engine/7000.0/script-reference/unity/entities/ijobchunk.md) pass the 'unfilteredChunkIndex' value from IJobChunk.Execute.**** (\[NativeArray\<Entity>]\(/engine/7000.0/script-reference/unity/collections/nativearray1)): The NativeArray of entities to have the component added.**** (\[ComponentType]\(/engine/7000.0/script-reference/unity/entities/componenttype)): The type of component to add.

### Remarks

At playback, if an entity already has this component, it will be skipped. Will throw an error if any entity is destroyed before playback, if component type is type Entity or [Prefab](/engine/7000.0/script-reference/unity/entities/prefab.md), or adding this componentType makes the archetype too large.

## AddComponent(int, Entity, ComponentTypeSet)

Records a command to add one or more components to an entity.

```csharp
public void AddComponent(int sortKey, Entity e, in ComponentTypeSet typeSet)
```

### Parameters

**** (\[int]\(https\://learn.microsoft.com/dotnet/api/system.int32)): A unique index for each set of commands added to this EntityCommandBuffer across all parallel jobs writing commands to this buffer. The [ChunkIndexInQuery](/engine/7000.0/script-reference/unity/entities/chunkindexinquery.md) provided by [IJobEntity](/engine/7000.0/script-reference/unity/entities/ijobentity.md) is an appropriate value to use for this parameter. In an [IJobChunk](/engine/7000.0/script-reference/unity/entities/ijobchunk.md) pass the 'unfilteredChunkIndex' value from IJobChunk.Execute.**** (\[Entity]\(/engine/7000.0/script-reference/unity/entities/entity)): The entity to get additional components.**** (\[ComponentTypeSet]\(/engine/7000.0/script-reference/unity/entities/componenttypeset)): The types of components to add.

### Remarks

At playback, it's not an error to include a component type that the entity already has. Will throw an error if this entity is destroyed before playback, if any component type is type Entity or [Prefab](/engine/7000.0/script-reference/unity/entities/prefab.md), or adding a component type makes the archetype too large.

## AddComponent(int, NativeArray\<Entity>, ComponentTypeSet)

Records a command to add one or more components to a NativeArray of entities.

```csharp
public void AddComponent(int sortKey, NativeArray<Entity> entities, in ComponentTypeSet typeSet)
```

### Parameters

**** (\[int]\(https\://learn.microsoft.com/dotnet/api/system.int32)): A unique index for each set of commands added to this EntityCommandBuffer across all parallel jobs writing commands to this buffer. The [ChunkIndexInQuery](/engine/7000.0/script-reference/unity/entities/chunkindexinquery.md) provided by [IJobEntity](/engine/7000.0/script-reference/unity/entities/ijobentity.md) is an appropriate value to use for this parameter. In an [IJobChunk](/engine/7000.0/script-reference/unity/entities/ijobchunk.md) pass the 'unfilteredChunkIndex' value from IJobChunk.Execute.**** (\[NativeArray\<Entity>]\(/engine/7000.0/script-reference/unity/collections/nativearray1)): The NativeArray of entities to have the components added.**** (\[ComponentTypeSet]\(/engine/7000.0/script-reference/unity/entities/componenttypeset)): The types of components to add.

### Remarks

At playback, it's not an error to include a component type that any of the entities already have. Will throw an error if any entity is destroyed before playback, if any component type is type Entity or [Prefab](/engine/7000.0/script-reference/unity/entities/prefab.md), or adding a component type makes the archetype too large.
