How do I add gameobjects to an array through a trigger?

I’m making a 2d RTS where you move units around and they would shoot at enemy units.
I want the player’s units to have an array that contains the enemies that are in its radius. And they will shoot the one at the top of the list. My question is, How can I do this through code?

    void OnTriggerEnter2D(Collider2D other)
    {
        if (other.gameObject.CompareTag("Enemy"))
        {
           
        }
    }
List<GameObjetcs> enemy=new List<GameObjetcs>();

void OnTriggerEnter2D(Collider2D other)
{
       if (other.gameObject.CompareTag("Enemy"))
{
enemy.Add(other.gameobject);
}
}
1 Like

enemy.Add does not exist in C#

System.Collections.Generic.List<GameObject> enemy = new System.Collections.Generic.List<GameObject>();

        void OnTriggerEnter2D(Collider2D other)
        {
            if (other.gameObject.CompareTag("Enemy"))
            {
                enemy.Add(other.gameObject);
            }
        }

it’s a List of C#.

1 Like

Oh. Thank you

1 Like