Object pooling is a process of reusing the same GameObject instance instead of repetitively creating and destroying it.

In this script you will find two different sets of methods for pooling objects one for GameObject and another for AudioClip components. Use them as per your use case.

Usage

Use object ObjectPooling.GetObject(prefab) instead of Instantiate(prefab) whenever you want to create or destroy a GameObject multiple times, When you no longer need the object use ObjectPooling.ReturnObject(prefab,instance) .

By default every visual/audio effect used by this framework has object pooling.

To use Object Pooling use the global namespace ObjectPooling.

objectPoolParent

If you wish you can add this script to a GameObject in your scene to have all the object pools in there Otherwise a GameObject will be created automatically.

Methods

static public GameObject GetObject(GameObject prefab, Vector3 position ,Quaternion rotation,Transform parent = null)

Use this method in most cases when you want to pool a GameObject.

It returns the instantiated GameObject

prefab (GameObject) - the object you want to pool.

position (Vector3) - the position for prefab in world space.

rotation (Quaternion) - the rotation for prefab in world space.

parent (Transform) - parent for prefab, by default this will be objectPoolParent.

static public GameObject GetObject(GameObject prefab)

Overload for GetObject, here the position and rotation will be zeroed and the parent will be **objectPoolParent.**