Skip to content

Instantly share code, notes, and snippets.

Last active March 28, 2024 22:21
Show Gist options
  • Save quill18/5a7cfffae68892621267 to your computer and use it in GitHub Desktop.
Save quill18/5a7cfffae68892621267 to your computer and use it in GitHub Desktop.
/// Simple pooling for Unity.
/// Author: Martin "quill18" Glaude (
/// Latest Version:
/// License: CC0 (
/// 2015-04-16: Changed Pool to use a Stack generic.
/// Usage:
/// There's no need to do any special setup of any kind.
/// Instead of calling Instantiate(), use this:
/// SimplePool.Spawn(somePrefab, somePosition, someRotation);
/// Instead of destroying an object, use this:
/// SimplePool.Despawn(myGameObject);
/// If desired, you can preload the pool with a number of instances:
/// SimplePool.Preload(somePrefab, 20);
/// Remember that Awake and Start will only ever be called on the first instantiation
/// and that member variables won't be reset automatically. You should reset your
/// object yourself after calling Spawn(). (i.e. You'll have to do things like set
/// the object's HPs to max, reset animation states, etc...)
using UnityEngine;
using System.Collections.Generic;
public static class SimplePool {
// You can avoid resizing of the Stack's internal data by
// setting this to a number equal to or greater to what you
// expect most of your pool sizes to be.
// Note, you can also use Preload() to set the initial size
// of a pool -- this can be handy if only some of your pools
// are going to be exceptionally large (for example, your bullets.)
const int DEFAULT_POOL_SIZE = 3;
/// <summary>
/// The Pool class represents the pool for a particular prefab.
/// </summary>
class Pool {
// We append an id to the name of anything we instantiate.
// This is purely cosmetic.
int nextId=1;
// The structure containing our inactive objects.
// Why a Stack and not a List? Because we'll never need to
// pluck an object from the start or middle of the array.
// We'll always just grab the last one, which eliminates
// any need to shuffle the objects around in memory.
Stack<GameObject> inactive;
// The prefab that we are pooling
GameObject prefab;
// Constructor
public Pool(GameObject prefab, int initialQty) {
this.prefab = prefab;
// If Stack uses a linked list internally, then this
// whole initialQty thing is a placebo that we could
// strip out for more minimal code. But it can't *hurt*.
inactive = new Stack<GameObject>(initialQty);
// Spawn an object from our pool
public GameObject Spawn(Vector3 pos, Quaternion rot) {
GameObject obj;
if(inactive.Count==0) {
// We don't have an object in our pool, so we
// instantiate a whole new object.
obj = (GameObject)GameObject.Instantiate(prefab, pos, rot); = + " ("+(nextId++)+")";
// Add a PoolMember component so we know what pool
// we belong to.
obj.AddComponent<PoolMember>().myPool = this;
else {
// Grab the last object in the inactive array
obj = inactive.Pop();
if(obj == null) {
// The inactive object we expected to find no longer exists.
// The most likely causes are:
// - Someone calling Destroy() on our object
// - A scene change (which will destroy all our objects).
// NOTE: This could be prevented with a DontDestroyOnLoad
// if you really don't want this.
// No worries -- we'll just try the next one in our sequence.
return Spawn(pos, rot);
obj.transform.position = pos;
obj.transform.rotation = rot;
return obj;
// Return an object to the inactive pool.
public void Despawn(GameObject obj) {
// Since Stack doesn't have a Capacity member, we can't control
// the growth factor if it does have to expand an internal array.
// On the other hand, it might simply be using a linked list
// internally. But then, why does it allow us to specify a size
// in the constructor? Maybe it's a placebo? Stack is weird.
/// <summary>
/// Added to freshly instantiated objects, so we can link back
/// to the correct pool on despawn.
/// </summary>
class PoolMember : MonoBehaviour {
public Pool myPool;
// All of our pools
static Dictionary< GameObject, Pool > pools;
/// <summary>
/// Initialize our dictionary.
/// </summary>
static void Init (GameObject prefab=null, int qty = DEFAULT_POOL_SIZE) {
if(pools == null) {
pools = new Dictionary<GameObject, Pool>();
if(prefab!=null && pools.ContainsKey(prefab) == false) {
pools[prefab] = new Pool(prefab, qty);
/// <summary>
/// If you want to preload a few copies of an object at the start
/// of a scene, you can use this. Really not needed unless you're
/// going to go from zero instances to 100+ very quickly.
/// Could technically be optimized more, but in practice the
/// Spawn/Despawn sequence is going to be pretty darn quick and
/// this avoids code duplication.
/// </summary>
static public void Preload(GameObject prefab, int qty = 1) {
Init(prefab, qty);
// Make an array to grab the objects we're about to pre-spawn.
GameObject[] obs = new GameObject[qty];
for (int i = 0; i < qty; i++) {
obs[i] = Spawn (prefab,, Quaternion.identity);
// Now despawn them all.
for (int i = 0; i < qty; i++) {
Despawn( obs[i] );
/// <summary>
/// Spawns a copy of the specified prefab (instantiating one if required).
/// NOTE: Remember that Awake() or Start() will only run on the very first
/// spawn and that member variables won't get reset. OnEnable will run
/// after spawning -- but remember that toggling IsActive will also
/// call that function.
/// </summary>
static public GameObject Spawn(GameObject prefab, Vector3 pos, Quaternion rot) {
return pools[prefab].Spawn(pos, rot);
/// <summary>
/// Despawn the specified gameobject back into its pool.
/// </summary>
static public void Despawn(GameObject obj) {
PoolMember pm = obj.GetComponent<PoolMember>();
if(pm == null) {
Debug.Log ("Object '""' wasn't spawned from a pool. Destroying it instead.");
else {
Copy link

Looks great!
Is there any reason why you chose an GameObject[] over List<GameObject>?

Copy link

If I am not totaly mistaken, using an Array has the advantage, that calling uppon specific elemts in the array (myArray[5] vs myList.ElementAt(5)) is faster. And because foreach-loops are slightly (but not much) slower than for loops it's easier in this case to work with the index

One of the big advantages of Lists is that you can resize it more efficently during runtime. But from what I can see this is not needed in that case so there goes that.

Other than that there shouldn't be too big of a difference.

Copy link

Thanks for this awesome script, this worked perfectly in my game.
Now I want to add one more feature in this, can you help me?

Basically I want to despawn all created elements means want to deactive all pool objects. At present using Despawn element, we can deactive element one by one. I want all at single method call.

I am waiting for your side suggestion.

Copy link


Copy link


I am having a problem. It is showing me KeyNotFoundException: The given key was not present in the dictionary. at line no. 196.

Copy link

If I am not totaly mistaken, using an Array has the advantage, that calling uppon specific elemts in the array (myArray[5] vs myList.ElementAt(5)) is faster. And because foreach-loops are slightly (but not much) slower than for loops it's easier in this case to work with the index

In DotNet a generic List<> is implemented as an array! See source.

Copy link

EricChen1248 commented Feb 6, 2017

Hey quill, thanks for this code, I made a small addition that I think it quite useful that you might want to add which you added outside in your project porcupine tutorial

I added
private GameObject parent;
to class Pool and in the constructor
parent = new GameObject( + "_pool");
Then under spawn I added a line
obj.transform.parent = parent.transform;

This will clean up the hierarchy in unity.

Copy link

devdabblr commented Mar 29, 2017

I'm getting inconsistent results when I call despawn. Sometimes the object despawns. Other times, it carries on being active. Any thoughts on why that is happening? I'm not receiving any errors.

Seems to happen when I've got multiple objects spawned from the list.

Thanks for the implementation, learned quite a bit through reading your code.

Copy link

wiiiteek commented Nov 3, 2017

Great work.

I have a sugestion. Maybe it would be good to add two messages "Spawn" and "Despawn". So in Pool.Spawn() method before the return there would be a line:

What do you think? ;)

Copy link

Awesome! Thank you.

Copy link

Draugor commented Jan 7, 2018

Hi Quill,

I changed a few thinks in your SimplePool so the pool doesn't need to Add/Get a unity component every time a new object gets added(instantiated) to the pool or gets despawned.
Instead i use a HashSet of GameObject.GetInstanceID() to lookup if the GameObject is in the pool or not.
Which gave me a performance boost significantly so when instantiating (growing) the pool.

All the changes are in my Fork if you want to take a look.

And also thanks for this really simple base anyone can just use !

Copy link

How do I use the SimplePool in my Update in my script ?

`using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PoolController : MonoBehaviour
public GameObject spawnPrefab;

private void Start()


private void Update()
    SimplePool.Spawn(spawnPrefab, Vector3.forward, Quaternion.identity);


private IEnumerator DestroyObjs()


What I want to do is when pressing for example on the Space key it will spawn some objects copies of the spawnPrefab and the destroy them some slowly like object pool like shooting effect. The main goal is to create kind of shooting effect when pressing on Space key.

If pressing once on space shoot once if pressing non stop on space shoot nonstop.

Copy link

How would one call Spawn within a Coroutine? I ask, as I'm looking to spawn an object after X seconds.

From what I can tell Coroutines do not support being called on an object that is not activated, which is what this script expects.

Copy link

Great job!

Copy link

JaxkDev commented Apr 19, 2020

5 Years on and I am learning from your videos because you don't skip straight to the 'best' methods you explain every step you make.

Copy link

And if you need the despawn after a delay without using dirty coroutines

/// <summary>
/// Despawn the specified gameobject back into its pool after a delay
/// </summary>
public static void Despawn(GameObject obj, int after)
    Task.Delay(after).ContinueWith(t=> UnityMainThreadDispatcher.Instance().Enqueue(() => Despawn(obj)));

Using UnityMainThreadDispatcher. Why ? Because you can't interact with UI outside main thread ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment