How obtain all "collider"

I wrote this code:

void OnTriggerEnter2D(Collider2D col){
if(!once && clicked){
for(int i = 0; i< col.Length; i ++)

  •   	once = true;*
  •   }*
  • }*
    but it doesn’t work…
    My idea is to do the function just one time (with “once” ) and i want to destroy all gameobjects that are near the object (using trigger)…
    What can i write to obtain all object that are near it ?
    Thank you so much and sorry for my poor english :slight_smile:

OnTriggerEnter2D is a method defined in MonoBehaviour (the base class) that takes one Collider2D as a parameter, it doesn’t take an array of colliders like you’re trying to do. You could override the method, but it won’t get called automatically by a trigger event.

Is there a reason why you want to do the method only “just one time”?

I’m not entirely sure what you want your code to do, but if you want to destroy every gameObject triggering the trigger, this will work fine:

void OnTriggerEnter2D(Collider2D col)
		Destroy (col.gameObject);

If you want to destroy objects within a certain area, you should check out the static methods in Physics2D (Unity - Scripting API: Physics2D), like OverlapAreaAll, OverlapCircleAll etc, which will return an array of the colliders of the objects inside the given area. You can then destroy these