How to stop OnTriggerStay2d from detecting collisions after the first one

Hi helpful people!

I am trying to create a beam which interact with an enemy as long as it is within the boundaries of the trigger collider attached to said beam. It works, how can I ignore triggers sent by other enemies if the beam is already interacting with one enemy? How can I ignore subsequent triggers if a first collision already happened? Thank you! Here is my code that works, but still interacts with multiple enemies:

 private void OnTriggerStay2D(Collider2D collision)                  
    {
        if(collision.tag == "Enemy" && isBeamOn == true)              
        {
            InteractWithEnemy(collision);
        }
        else if (collision.tag == "Enemy" && isBeamOn == false)
        {
            FreeEnemy(collision);
        }
    }

    private void OnTriggerExit2D(Collider2D collision)                  
    {
        if (collision.tag == "Enemy")                                     
        {
            FreeEnemy(collision);                                         
        }
    }
    

    private void InteractWithEnemy(Collider2D collision)
    {
        playerMover.isInteractingWithEnemy = true;

        enemy = collision.gameObject;
        enemyMover = enemy.GetComponent<EnemyMover>();      
        enemyTransform = enemy.GetComponent<Transform>();  
        enemyRb = enemy.GetComponent<Rigidbody2D>();      

        // Interactions with enemy
    }

    private void FreeEnemy(Collider2D collision)
    {
        playerMover.isInteractingWithEnemy = false;

        enemy = collision.gameObject;
        enemyMover = enemy.GetComponent<EnemyMover>();      
        enemyMover.isFree = true;  
		
	// Release the enemy
    }

Hi , the easiest way is having a global boolean.

     bool enemyTrigger = false;
     private void OnTriggerStay2D(Collider2D collision)                  
         {
            if(enemyTrigger)
                     return;
             if(collision.tag == "Enemy")              
             {
                 InteractWithEnemy(collision);
                enemyTrigger  = true;
             }
     
         }
     private void OnTriggerExit2D(Collider2D collision)                  
     {
         if (collision.tag == "Enemy")                                     
         {
             FreeEnemy(collision);           
             enemyTrigger = false;                              
         }
     }

Hope this help you.

As an update, I did not resolve the issue while using OnTriggerStay2D. I followed Atiyeh123’s suggestion of a global boolean, but changed the interaction from being called from within the OnTriggerStay2D method to the Update, and by setting the object of the interaction from within OnTriggerEnter2D.