Recycling objects in tower defense

i heard on the googles about recycling objects so i started to give it a try on my tower defense scene. i was able to get a small array of enemies to recycle pretty well with one global array. now im about to do the bullets in the scene, but it seems like all the towers trying to access one array would be almost impossible to track.

should i give each turret a small array of bullets for their own personal clip?
or would one world clip be better once i understood how to manage multiple turrets accessing it it?

i had another quick question, i just added a little plasma explosion to my turrets impacts and i was wondering: does something as small as an empty object with a particle script need to be recycled or is it ok to let that be created and destroyed alot?
it is created ALOT though, every wave has 20 enemies right now, and each one takes 10-15 shots, so im making a crap load of those little objects with every wave…

i would try to put the particles on my bullets but they disappear right after collision so you wouldn’t see that particles

I should imagine that for small objects instantiating and destroying wouldn’t have much overhead but if you want to optimize it further I would say have a global queue of bullets (Queue Class (System.Collections) | Microsoft Learn) and the bullet could be two objects parented to the bullet, one for the graphic of the bullet and one for the particle effect, So when the turret fires you take a bullet out of the queue (using dequeue) and when the bullet collides with something you disable the graphic and enable the particle effect, then when the particle effect has finished put the bullet back in the queue (using enqueue).

ohh, that queue system looks really cool but im still learning scripting as i go, might take me a while to understand XD
thanks for the tips :slight_smile:

No worries, feel free to pm me if you need any further help with queues etc.

creating GameObjects is actually a kind of costly process and you can sometimes see slow-down created by it if you’re creating them in something like the Update loop.

Personally I use a ‘spawn pooling’ system that lets you pool a cache of objects into various pools (you can have global pools, or local pools attached to special gameobjects). When you want to create an instance of something you ask the pool to give you one that is available. If there are none available it will resize the cache to make some new ones available. The resize amount is controllable based on the gameobject you’re trying to spawn.

I’ve see several unity packages that do this for you. I personally wrote my own just because I wanted certain features that the packages didn’t have… that and I thought it was fun.

1 Like

use a pooling system for all of the objects that you will “create” and “destroy” often, since the GC in unity is terrible.

that sounds exactly like what im looking for, im gonna go look that up.
thx !

just made myself a really awesome double gatling turret in blender, and i got it to use only one ‘bullet’. its just an empty game object that pops onto whoever i just shot and shoots some sparks out, kinda like how marines shoot in Starcraft. hows THAT for efficiency :slight_smile:
scripting kicks ass

but for turrets with visible projectiles i’ll definitely try to pool them. thanks for the tips, if i had some better Google skills i might be able to help myself but i’m terrible at Google, so i appreciate you guys taking the time to help me.