Pooling Part 2

Pooling Part 2: How to use the pool (incl. Demo project)

In the first part 04-arrow-northeast of the pooling series I explained why you should use pooling and did a simple implementation of a pool class using a stack. In this post I’m going to show you an example how to use the prior work in your project.

For this we create a scene with an endless amount of flying cubes that are spawned at the right border of the screen and will fly to the opposite side.

Something that looks like this:

Ok, let’s do this

Given you’ve got the code from the previous part of the pooling tutorial, we only need a prefab and an additional script for this one to work.

For the prefab you can go with whatever you want. I went with the red cubes you see above.

The script is called Spawner.cs and implements two methods: Spawn() and Update(). To create a new cube at the right side of the screen the Spawn() method is called every x seconds.  The Update() method updates the position and rotation of all flying cubes.

See the code:

Unity demo project

You can download the whole Unity project here. Download

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.