Need help with Tower Defense Targeting system

Hi, I have run into some problems with my Tower Defense game I’m currently creating. What I want to do is to create a properly working targeting system for my towers. So far I have a collider on my tower that works as the radius I can shoot. The problem with this is that it only register a target on enter and exit and not if some target are inside the collider. You might me thinking “but that should work fine” but that is not the case because if i where to have a target and another targets enter and then the first target die the tower wont target the other target so what i was thinking of was maybe to do a list of some sort that register all targets that enter the collider and removes them from the list if they exit, and then have the tower target the next target on the list. The problem with that is that i have no idea of how to do that so if you guys could help me out i would be very very grateful.

OnCollisionStay() should help you out: Unity - Scripting API: MonoBehaviour.OnCollisionStay(Collision) it will tell you what collisions are currently inside the trigger. I’ve never used it personally, so I’m not sure exactly how it works
another thing you could do is create an array and overtime OnTriggerEnter() is called, add the triggering game object to the array and have the tower attack the first object in the array

Thanks for the quick reply, I will try that out, i have also thought about the array thing but I’m not to familiar to arrays so I’m not really sure how i would set it up. the thought that comes to my mind is in Trigger enter add one to the array and in trigger exit remove it?

I did a blog post with a script some time ago.

What you want to do is use a collection to keep track of the targets. Add each one in on trigger enter, remove it in on trigger exit.

Both a List and a HashSet would work as collections.

Performance wise on trigger stay is a bad idea.

Yes that was what i thought about and i also got it working. The only problem now is to remove to object from the array if it dies and i cant get that working. Got any ideas?

To save typing it all out again I’ve found the original blog post. Let me know if this doesn’t cover your question.