# CreateBodyBatch

> Create a batch of bodies in the world.

## Definition

* **Type:** Method
* **Namespace:** [Unity.U2D.Physics](/engine/6000.7/script-reference/unity/u2d/physics.md)
* **Assembly:** UnityEngine.PhysicsCore2DModule

## CreateBodyBatch(PhysicsBodyDefinition, int, Allocator)

Create a batch of bodies in the world.

```csharp
public NativeArray<PhysicsBody> CreateBodyBatch(PhysicsBodyDefinition definition, int bodyCount, Allocator allocator = Allocator.Temp)
```

### Parameters

**** (\[PhysicsBodyDefinition]\(/engine/6000.7/script-reference/unity/u2d/physics/physicsbodydefinition)): The body definition to use.**** (\[int]\(https\://learn.microsoft.com/dotnet/api/system.int32)): The number of bodies to create.**** (\[Allocator]\(/engine/6000.7/script-reference/unity/collections/allocator)): The memory allocator to use for the results. This can only be [Allocator.Temp](/engine/6000.7/script-reference/unity/collections/allocator/temp.md), [Allocator.TempJob](/engine/6000.7/script-reference/unity/collections/allocator/tempjob.md) or [Allocator.Persistent](/engine/6000.7/script-reference/unity/collections/allocator/persistent.md).

### Returns

| Type                                                                                           | Description                                                                                                                                    |
| ---------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------- |
| [NativeArray\<PhysicsBody>](/engine/6000.7/script-reference/unity/collections/nativearray1.md) | The created bodies. This NativeArray must be disposed of after use otherwise leaks will occur. The exception to this is if the array is empty. |

### Remarks

Use this method to create many bodies that share the same configuration, instead of calling [PhysicsWorld.CreateBody](/engine/6000.7/script-reference/unity/u2d/physics/physicsworld/createbody.md) in a loop.

### Examples

```csharp
// Create 500 bodies using a single definition.
using UnityEngine;
using Unity.Collections;
using Unity.U2D.Physics;

public class CreateBodyBatchExample : MonoBehaviour
{
    public PhysicsBodyDefinition bodyDefinition = PhysicsBodyDefinition.defaultDefinition;

    void Start()
    {
        PhysicsWorld world = PhysicsWorld.defaultWorld;
        using NativeArray<PhysicsBody> fiveHundredBodies = world.CreateBodyBatch(bodyDefinition, 500);
    }
}
```

## CreateBodyBatch(PhysicsBodyDefinition, int, Object, int, Allocator)

Create a batch of bodies in the world, assigning the given owner to every created body.

```csharp
public NativeArray<PhysicsBody> CreateBodyBatch(PhysicsBodyDefinition definition, int bodyCount, Object owner, int ownerKey, Allocator allocator = Allocator.Temp)
```

### Parameters

**** (\[PhysicsBodyDefinition]\(/engine/6000.7/script-reference/unity/u2d/physics/physicsbodydefinition)): The body definition to use.**** (\[int]\(https\://learn.microsoft.com/dotnet/api/system.int32)): The number of bodies to create.**** (\[Object]\(/engine/6000.7/script-reference/unityengine/object)): The owner object to assign to every created body. If null, no owner is assigned.**** (\[int]\(https\://learn.microsoft.com/dotnet/api/system.int32)): The owner key to assign. Must be non-zero when owner is not null. See [PhysicsWorld.CreateOwnerKey](/engine/6000.7/script-reference/unity/u2d/physics/physicsworld/createownerkey.md).**** (\[Allocator]\(/engine/6000.7/script-reference/unity/collections/allocator)): The memory allocator to use for the results. This can only be [Allocator.Temp](/engine/6000.7/script-reference/unity/collections/allocator/temp.md), [Allocator.TempJob](/engine/6000.7/script-reference/unity/collections/allocator/tempjob.md) or [Allocator.Persistent](/engine/6000.7/script-reference/unity/collections/allocator/persistent.md).

### Returns

| Type                                                                                           | Description                                                                                                                                    |
| ---------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------- |
| [NativeArray\<PhysicsBody>](/engine/6000.7/script-reference/unity/collections/nativearray1.md) | The created bodies. This NativeArray must be disposed of after use otherwise leaks will occur. The exception to this is if the array is empty. |

### Remarks

See [PhysicsBody.SetOwner](/engine/6000.7/script-reference/unity/u2d/physics/physicsbody/setowner.md).

## CreateBodyBatch(ReadOnlySpan\<PhysicsBodyDefinition>, Allocator)

Create a batch of bodies in the world.

```csharp
public NativeArray<PhysicsBody> CreateBodyBatch(ReadOnlySpan<PhysicsBodyDefinition> definitions, Allocator allocator = Allocator.Temp)
```

### Parameters

**** (\[ReadOnlySpan\<PhysicsBodyDefinition>]\(https\://learn.microsoft.com/dotnet/api/system.readonlyspan-1)): The definitions used to create the bodies. The number of bodies produced is implicitly controlled by the number of definitions in this span.**** (\[Allocator]\(/engine/6000.7/script-reference/unity/collections/allocator)): The memory allocator to use for the results. This can only be [Allocator.Temp](/engine/6000.7/script-reference/unity/collections/allocator/temp.md), [Allocator.TempJob](/engine/6000.7/script-reference/unity/collections/allocator/tempjob.md) or [Allocator.Persistent](/engine/6000.7/script-reference/unity/collections/allocator/persistent.md).

### Returns

| Type                                                                                           | Description                                                                                                                                    |
| ---------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------- |
| [NativeArray\<PhysicsBody>](/engine/6000.7/script-reference/unity/collections/nativearray1.md) | The created bodies. This NativeArray must be disposed of after use otherwise leaks will occur. The exception to this is if the array is empty. |

## CreateBodyBatch(ReadOnlySpan\<PhysicsBodyDefinition>, Object, int, Allocator)

Create a batch of bodies in the world, assigning the given owner to every created body.

```csharp
public NativeArray<PhysicsBody> CreateBodyBatch(ReadOnlySpan<PhysicsBodyDefinition> definitions, Object owner, int ownerKey, Allocator allocator = Allocator.Temp)
```

### Parameters

**** (\[ReadOnlySpan\<PhysicsBodyDefinition>]\(https\://learn.microsoft.com/dotnet/api/system.readonlyspan-1)): The definitions used to create the bodies. The number of bodies produced is implicitly controlled by the number of definitions in this span.**** (\[Object]\(/engine/6000.7/script-reference/unityengine/object)): The owner object to assign to every created body. If null, no owner is assigned.**** (\[int]\(https\://learn.microsoft.com/dotnet/api/system.int32)): The owner key to assign. Must be non-zero when owner is not null. See [PhysicsWorld.CreateOwnerKey](/engine/6000.7/script-reference/unity/u2d/physics/physicsworld/createownerkey.md).**** (\[Allocator]\(/engine/6000.7/script-reference/unity/collections/allocator)): The memory allocator to use for the results. This can only be [Allocator.Temp](/engine/6000.7/script-reference/unity/collections/allocator/temp.md), [Allocator.TempJob](/engine/6000.7/script-reference/unity/collections/allocator/tempjob.md) or [Allocator.Persistent](/engine/6000.7/script-reference/unity/collections/allocator/persistent.md).

### Returns

| Type                                                                                           | Description                                                                                                                                    |
| ---------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------- |
| [NativeArray\<PhysicsBody>](/engine/6000.7/script-reference/unity/collections/nativearray1.md) | The created bodies. This NativeArray must be disposed of after use otherwise leaks will occur. The exception to this is if the array is empty. |

### Remarks

See [PhysicsBody.SetOwner](/engine/6000.7/script-reference/unity/u2d/physics/physicsbody/setowner.md).
