I'm working on a 2D tower defense game and I'm not sure how to make the turrets get a new target. I want them to focus the enemy that is closest to the end. And I wonder, what is the most efficient way of detecting all objects in a range of the turret, each time the current target dies or leaves range.
I'm storing all enemies that enter the collision with the turret in a list.
Storing all the enemies in a list and iterating over them, selecting the closest one, would be the simplest implementation of this. Even if you had many enemies, the performance impact of this would be negligible. Optimizing before you've written the code is pointless.
Another option would be Physics2D.OverlapCircle
to get all nearby enemies, but you would still have to iterate over them.