Cleaning the Object Pool

For a little backstory:

I am using object pooling in my current project for the player bullets, enemies, and power ups. The problem was that every time my player changed weapons, the Pool Manager would instantiate more bullets, adding to the existing bullets in the hierarchy. So, depending on how many times the player weapon is upgraded and downgraded, the hierarchy could have a ton of bullets, with only the most recently instantiated getting used.

I think I had about 60 bullets at one point, with only the most recently instantiated being used…

My Solution:

After doing a bit of research on how to clean up the objects in the hierarchy and only finding ways to set them inactive, not destroy them, I decided on a solution. First, I created a new C# script called ClearChildren. Then, I attached it to the game object that holds the instantiated bullets in my hierarchy(for me it’s “BulletContainer”).

Only the current bullet is pooled.



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