How to detect what Gameobjects are in a Collider2D? (C#)

I am making a tower defense game (2D) and I am working on making the towers shoot the enemies. I want my turrent to shoot every 1 second and hit the enemy the closest to the front. My solution to this problem is by using circle colliders to detect if an enemy is in firing range, but I can not figure out how to shoot the enemy in first. I am stuck on trying to figure out how to put the enemies that are in the turrents range/collider2d in a list/enumerator/array (Whichever one is appropriate for storing gameobjects and can change length).

I looked through the documents and I couldnt find any collider2D function that lists the gameobjects in its range, so i need an alternative way to do it.

you use collider and rigidbody into the bullet?

You may not have searched well : Physics.OverlapSphere I guess you will be able to use it for 2d games to.

Though, this is not the best way to go if you plan to do it every frame. Adding an enemy into a list when it enters the trigger, and remove it when it leaves the trigger is a better solution in my opinion.

List is the more appropriate data structure for your problem in my opinion :

  • Array : You will have to resize it my hand or declare a large array and hope a few number of enemy will enter the collider
  • Enumerator : Never used, but it seems quite complicated for what you are trying to do
  • List : Simple to use, flexible, quite performant