Nerd For Tech
Published in

Nerd For Tech

Galaxy Shooter 2D — Object Pooling #23

Recycling GameObjects can greatly improve performance!

Object Pooling is a common technique used in the videogame industry to increase performance by reusing the same instances of an entity and storing them when they are no longer being used.

In my game, there are infinite waves of enemies, and tons of lasers to be instantiated by the enemies and the player, and constantly spawning and destroying GameObjects will create overhead in the Garbage Collector, thus, causing performance spikes.

In this article, I’m going to prevent that by using Object Pooling.

How I implemented it

  • A common Object Pool has the following variables:
    - The GameObject prefab to instantiate and store.
    - The number of copies of that prefab to initialize the pool.
    - A list of all the Pooled GameObjects, where it will check if there are any available objects to use.
  • I’ve made some changes to the original formula to fit the ObjectPool structure I have in mind.
  • Now, the real ObjectPool class is technically the whole manager of every pool available. So, it’s necessary for it to be a Singleton, and it should have a List of all the Pools, and a Dictionary to get them quick and easy.
That enum has some big spoilers for the future
  • On Start, I call a method named PopulatePools, which, as the name suggests, fills the pools with an amount of their respective GameObjects.
  • The GenerateNewObject method instantiates the desired GameObject into its respective Pool, and adds it to the Pool List. In case the Pool runs out of GameObjects, I have set the method as a GameObject return type.
  • Now, I needed a public method to request a Pooled GameObject. This method will return one deactivated GameObject and set it active with a new position and rotation. If there are no available GameObjects in the pool, generate one and return that instead.
    Usually, position and rotation is handled by the script that asked for the GameObject. However, it seemed more appropriate for me to set those properties in the Object Pool Manager, and avoid repeating the same lines of code in every script.
  • Finally, those scripts that may want to use a Pooled GameObject will need to access the Object Pool Manager instance to request a GameObject.
The last line of code is how it gets a Player Laser from the Object Pool.

Quick Note

All the GameObjects that are inside a Pool must have a script with functionality to deactivate themselves. The only way to reuse them is to do that.

Also move your initialization behaviour inside the OnEnable method (called every time the GameObject is activated) instead of Start (only called once). You can also use OnDisable to clear values or do something before the GameObject gets disabled.

Result

Object Pool Manager in the Editor

Object Pool in Action

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store