Simple Pooling for GameObjects in Unity

Turns out adding in Object Pooling isn’t as easy as I would have hoped. Most of my issues turned out to revolve around not being able to access interfaces directly on GameObjects. Here is a quick sample of my final object pooling running:


As you can see I am now spawning objects and each one is created from scratch, assuming there isn’t an instance already in the scene. The key here is that when a GameObject runs off the screen I disable it by looking for the following script:

using UnityEngine;
using System.Collections;
public class RecycleGameObject : MonoBehaviour, IRecycle {
	public virtual void Reset(){
	public virtual void Dissable(){

Now my Objects imply implement this script and override the two methods Reset and Dissable to achieve the desired results. Now when an object goes offscreen, I request the RecycleGameObject script like so:

void OnOutOfBounds(){
	var recycleScript = GetComponent<RecycleGameObject> ();
	if (recycleScript == null) {
		Destroy (gameObject);
	} else {

To my surprise, looking for RecycleGameObject components also return any component that extends it so now I can easily shut down an object offscreen and my pool class returns any disabled instances from its cache.

Subscribe To My Mailing List

Want to learn how to make a game? Not sure where to start? Even if you are a seasoned game maker there is still a lot you can learn from my mailing list. I'll be covering tips and tricks for how to build, release and market games each month.

Simply sign up for my mailing list and also get access to a 50% off discount code for my eBooks and other content. I promise to not spam your inbox!

Join Now