Nerd For Tech

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences…

Follow publication

Member-only story

Object Pooling Design Pattern in Unity

Object pooling is a design pattern that will allow you to reduce the amount of garbage collection you have when working with instantiating multiple objects. One of the easiest examples of when this could be used is for bullets in a shooter game.

To set this up you will create an empty game object that will be the pool manager for your game. You will also create the pool manager script and attach it to the pool manager game object. You will also want to make a game object that will be used to hold the bullets to keep the hierarchy clean.

Make the pool manager script a singleton to allow all scripts easy access to it.

Create a reference to a prefab. Create a list to hold the prefabs and a variable to tell the game how many prefabs to instantiate. You can also make a container prefab for the bullet container.

Create a method that will return as a list. This method will take an integer parameter, loop through, and create a prefab based on the integer given. Once a bullet is instantiated it will be parented to the container and disabled.

Nerd For Tech
Nerd For Tech

Published in Nerd For Tech

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/.

Matthew Clark
Matthew Clark

No responses yet

Write a response