> For the complete documentation index, see [llms.txt](https://heroicsolo.gitbook.io/heroic-engine/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://heroicsolo.gitbook.io/heroic-engine/basics/engine-utilities/poolsystem.md).

# PoolSystem

This class provides static methods for getting gameobjects and returning them back to pool.

{% hint style="info" %}
Instance pooling is very good practice to squeeze better performance in case if you need to spawn objects frequently (like projectiles in shooter game or something like this).
{% endhint %}

### Available methods

```csharp
public static void ResetPools(bool destroyImmediately = true)
```

*This method clears all created pools.*

***

```csharp
public static T GetInstance<T>(T prefab, string name) where T : MonoBehaviour, IPooledObject
```

*This method returns an instance of class T which is inherited from MonoBehaviour and IPooledObject, and presented by certain **prefab**. It tries to get such object from pool with assigned **name**,* *in case if such pool isn't found, it creates new pool with this **name**.*

*Example:*

```csharp
var resourceUISlot = PoolSystem.GetInstance(resourceSlotPrefab, resourceSlotPrefab.GetName());
```

***

```csharp
public static T GetObj<T>(string name) where T : MonoBehaviour, IPooledObject
```

*This method returns an available instance of class T which is inherited from MonoBehaviour and IPooledObject, and contained in pool with **name**.*

*Example:*

<pre class="language-csharp"><code class="lang-csharp"><strong>var explosion = PoolSystem.GetObj&#x3C;PooledParticleSystem>("Explosion");
</strong>
explosion.transform.position = targetPos;
</code></pre>

***

```csharp
public static void ReturnToPool<T>(T obj) where T : MonoBehaviour, IPooledObject
```

*This method returns gameobject **obj** of given type **T** to pool. This gameobject will be immediately deactivated after this.*

*Example:*

```csharp
enemy.GetDamage(bullet.Damage);
bullet.Explode();

PoolSystem.ReturnToPool(bullet); //We return projectile back to pool after collision
```

***

```csharp
public static T GetInstanceAtPosition<T>(T prefab, string name, Vector3 pos, Transform parent = null) where T : MonoBehaviour, IPooledObject
```

*This method returns an available instance of given **prefab** from pool with **name**, moves this instance to given world position and sets certain **parent** transform. If parent is null, it leaves instance in previously assigned parent transform.*

*Example:*

```csharp
var bullet = PoolSystem.GetInstanceAtPosition<Projectile>(bulletPrefab,
    bulletPrefab.GetName(),
    muzzleTransform.position);

bullet.Launch(targetTransform.position);
```

***

```csharp
public static T GetInstanceAtPosition<T>(T prefab, string name, Vector3 pos, Quaternion rotation, Transform parent = null) where T : MonoBehaviour, IPooledObject
```

*This method returns an available instance of given **prefab** from pool with **name**, moves this instance to given world position, rotation and sets certain **parent** transform. If parent is null, it leaves instance in previously assigned parent transform.*

*Example:*

```csharp
var bullet = PoolSystem.GetInstanceAtPosition<Projectile>(bulletPrefab,
    bulletPrefab.GetName(),
    muzzleTransform.position,
    Quaternion.LookRotation(targetTransform.position - muzzleTransform.position));

bullet.Launch(targetTransform.position);
```
