# 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);
```


---

# 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://heroicsolo.gitbook.io/heroic-engine/basics/engine-utilities/poolsystem.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.
