Object Pool Package

Name: Object Pool - Adam
By AdamO on Discord

The Object Pool package is a simple solution to handle object pooling. Object pooling can massively help the performance of your games, if you spawn a lot of repeated templates (e.g. lots of pickups).

What is object pooling?

  • Object pooling is a design method in games, where rather than constantly spawning new objects (which is expensive), you spawn a “pool” of inactive objects when the game starts, and then simply use (activate) them when needed, and return (deactivate) them when you’re done with them (which is much more performant).
  • Imagine a game where you shoot lots of grenades from a launcher. With 20 players all firing, you might be spawning 20 grenades every second, which only last a few seconds before exploding - this is very expensive and remains expensive through all your gameplay, potentially ruining your performance.
  • With object pooling, you can create a “pool” of 100 grenades when the game starts, then just use and return those grenades each time a player fires, never having to spawn any new ones - this is expensive when the game first loads, but is then much cheaper.
  • For more information, see this tutorial - OBJECT POOLING in Unity - YouTube

Example template

  • Drop the “ObjectPool” template into your game. It’s recommended that you keep object pools far away from gameplay (e.g. [0,0,-10000]).
  • Reference the ObjectPool entity in a property, and the PoolApple template in a property.
  • To use a “PoolApple” entity, simply try local newApple = self.properties.objectPool.objectPoolScript:GetAvailableObj(self.properties.appleTemplate).
  • Then, rather than destroying the apple when you’ve used it, call self.properties.objectPool.objectPoolScript:ReturnObj(newApple).

To use:

  • Create a pool for your template(s). A single pool script can take an array of templates to create pools for, but you can also use multiple object pools to help you organise, like in my example below:


  • Assign templates to the “templates” array for the objects you want to create a pool of. You’ll only be able to retrieve instances of those templates from this pool.
  • Set your starting amount. The pool will spawn this many of each template when the game starts, but don’t worry - if your game tries to use more than what’s available in a pool, it’ll spawn more to top it up. Try to estimate here how many you’ll need - the closer you estimate, the better your game will perform.

  • The 3 events here can be called when objects are created (that is, when they’re initially spawned into the pool), used, and returned. Each is sent with the entity as the argument. By default on this template, they’ll call each function on the other attached script, the “poolStateManager”.
  • The pool state manager handles the object’s state when created, used and returned. It relies on a stateSwitcherScript (State Switcher package) to toggle the object’s visibility and collision. If you untick “enableCollision”, the collision of your object won’t be affected - bear in mind that invisible objects in your pool could be collidable.
  • The pool state manager script also sends “OnEnabled” to the objects when they’re used, and “OnDisabled” when they’re returned. You could use these functions on the objects, to reset them in some way, or play animations when they “spawn”.