# Using the Built-in Object Pooling

{% hint style="warning" %}
**Important:** It is expected that you have C# coding knowledge before proceeding.
{% endhint %}

## Emerald AI Built-in Object Pool

Emerald AI has built-in object pooling allowing Emerald AI projectiles and effects to automatically be recycled so they don't have to be instantiated every time they're used. Users can also utilize the object pooling system to spawn custom objects and AI to help improve performance by not always instantiating the object.

**Note:** The **`using EmeraldAI.Utility;`** namespace needs to be added at the top of your script in order for the EmeraldObjectPool script to be visible.

{% hint style="warning" %}
**Important:** Objects created with Emerald AI's object pooling need to be despawned and cannot be destroyed using Unity's Destroy(gameObject); code. Be sure to see the Despawning an Object below for the code to do this.
{% endhint %}

### Spawning an Object

To spawn an object with Emerald AI's object pooling system, you will need to use the following code. If you want the object to be added back to the pool, you will need to call Despawn. This is covered below.

```csharp
EmeraldObjectPool.Spawn(ObjectToSpawn, SpawnPosition, ObjectRotation);
```

### Spawning an Object with Auto Despawn

To spawn an object with Emerald AI's object pooling system that will automatically despawn after the specified seconds, you will need to use the following code:

```csharp
EmeraldObjectPool.SpawnEffect(ObjectToSpawn, SpawnPosition, ObjectRotation, DespawnSeconds);
```

### Despawning an Object

In order for a spawned object to be added back to the object pool, you will need to use the following code. If you are using auto despawn as explained above, despawn doesn't need to be called as it's done so automatically.

```csharp
EmeraldObjectPool.Despawn(ObjectToDespawn);
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://black-horizon-studios.gitbook.io/emerald-ai-wiki/api/using-the-built-in-object-pooling.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
