I am making a bullet-hell game with object pooling where each projectile would contain a script that contains multiple types of behaviors. Typically several hundreds of projectiles exist on screen at once.
Data for which behavior should be used needs to be set when the projectile is created or set active from within the object pooling script. However, I do not want to hurt my performance with a GetComponent call to the given projectile’s script every time I need to ‘create’ a new one.
The solution I have come up with is to instead set the projectile’s y-value, which would be unused given the nature of the game, and for the script to then calculate which behavior needs to occur based on the object’s y-transform. It seems to be the most performant solution to the problem, however it is very cumbersome.
The other option I have considered is instead turning my List of pooled objects into a List of object-script pairs, where I use GetComponent on the creation of the pooled object and then get access to the script variable without a GetComponent call associated with it every time thereafter.
The question I have is what the implications of the second option are for performance. Is there any performance cost for this implementation beyond the initial GetComponent call given that the script would seemingly need to be constantly updated for parity in the object pooler?
If you are asking performance questions, if feels like maybe you haven’t launched the profiler yet.
Window → Analyze → Profiler
If you haven’t done this, are you even sure you NEED a pooling system? It’s fine if you’re just doing it to learn, but you will make a total wreck of your project forever and into the future if you add pooling when you don’t really need it.
The costs and issues associated with object pooling / pools:
Your post reads as if you think Instantiate can only return a GameObject but it can return a reference to any component on the object. You don’t need to retrieve the reference after you create it.