Object Pool and components

Hi, I am using a simple object pool for use with drawing generic objects such as lines, rectangle, circles, curves, etc. Sometimes I add other components to them such as BoxCollider2D, FollowMouse, etc. The question I have, what would be the best way of handling it?

Right now I add the components when I need them then destroy them when I put the object back into the inactive list but after coding this I thought, this kind of defeats the purpose of an object pool if I am just going to create and destroy the components but not the game objects.

What would be the best way to eliminate the need to destroy components after creation. Also, it is not viable to create a pool for each type of object because sometimes I add components outside of the initial script the pool is in so I cannot think of every combination of components that I might need.

Thanks in advance for any help!

True, there is no purpose for you to use an object pool if you are going to be destroying them. Typically an object pool has a Create method that initializes a component’s variables and a Release method that disables the game object and/or component and resets its position to a default location.

But the question is, why do you think you need an object pool for everything? Are you going to be creating different components that each has more than 1000 objects in the scene?

Remember, an object pool is for pooling a single “type” of entity. I.e. if you have 1000 bullets, then you have one bullet prefab and an object pool that controls those prefabs (Creates and Releases when appropriate). That bullet prefab can have any number of components. The object pool will make sure to config each component (based on your parameters) when creating.

Now, for something like a player game object, or even an enemy game object, that you will most likely only have a few in the scene, there’s no need for an object pool.