Object pooling

As soon as you implement a pooling system, all of a sudden YOU become responsible for writing specific code zeroing out everything on those recycled objects.

Sounds easy if they start out as “just a bullet” or something simple.

But then you add a Rigidbody… oh now you need to have code that will set .velocity and .angularVelocity to zero before recycling the object. Oh wait, did I change the .drag field and .inertialTensor fields to get my boomerang feeling right? Now I have to reset that.

And then use the pooling for an enemy with AI and animation… now the animation system has to be reset (forced to instant-transition to the known initial state), the AI has to be reset, and you forgot to zero out some aggro counter and now you have this weird subtle bug where recycled enemies are a little more dangerous… or their first frame shows them in the last pose they were in before they got recycled.

And on and on… NavigationMeshAgent? What if you change the arrival distance for certain enemy types in certain conditions… and you forgot to reset it… “Bug: some enemies cannot reach the door.” etc. etc. etc.

Seriously, trust me, pooling objects can get really really complicated unless you are very disciplined about what objects get recycled and what state they have.

You will need to maintain and debug and grow that reset code for EVERY different flavor of thing in play now, plus EVERY NEW THING forever into the future if it is on a pooled object.

And in the end… oh look, it isn’t even any faster anyway because Unity3D is already a beast of an engine!

7 Likes