How to end a for loop that is inside of an IEnumerator and is being called from another script

In my game I am trying to increase a number(ammo) while the player is standing under and beam of light. So I have an OnTriggerEnter2D on my player that detects for when the player is touching the beam of light. When the player is touching the beam it starts a Coroutine in another script that starts a for loop to add ammo every second. Then in my OnTriggerEnter2D it also detects if the player isnt touching the beam and is supposed to end the Coroutine with the for loop. However the for loop will not end no matter what until it is finished.
This script is on my player:

 public void OnTriggerEnter2D(Collider2D collider)
    {
        Debug.Log(collider);
        if (collider.gameObject.name == "Sunlight")
        {
                StartCoroutine(other.BeamCount(true));
        }
        if (collider.gameObject.name != "Sunlight")
        {         
                StartCoroutine(other.BeamCount(false));
        }
    }

This script is on the object where my bullets are spawned:

public int beamCount;

 public IEnumerator BeamCount(bool check)
    {
        Debug.Log(check);
        if (check == false)
        {
            yield return beamCount;
        }
        if (check == true)
        {
            for (int i = 0; i <= 50; i++)
            {
                beamCount = i;
                Debug.Log(beamCount);
                yield return new WaitForSeconds(1);
            }
        }
    }

If anyone could figure out how to end the for loop when the player is not touching the beam it would be very much appreciated. Thank you in advanced.

It sounds like you have really overcomplicated this. Why not just have a while loop or add an if statement that checks if the player is under your healing ray?


Other than that there are probably better design patterns that could be employed to write this code. i.e. players conform to an interface, on entering the ray either by using a sphere cast, on trigger enter or equivalent you test to see if that object conforms to that interface and if so on a timed interval you add a set amount of points to that players health field.

This method would scale across multiple players using the same health area at once, and wouldnt take half as much code.

yield break; //exits an ienumerator.

but you might want to re-think the logic here a little. seems a little over complex
something like this should work…
Should allow player to enter and edit the light, to get charge, until either the player is full or the charger is out of charge.

while(check && chargeLeft>50 && playerBullets<max )   // while player under light, and charge remaining, and player not full, then give to player
{
    playerBullets ++;
    chargeLeft --;
    yield return null;
}

Hey everyone, thank you all for all the help but I have found the answer!

 public void OnTriggerStay2D(Collider2D collider)     
    {
        if (collider.gameObject.name == "Sunlight")
        {
            Debug.Log(beamCount);
            currentBeamTime += Time.deltaTime;
            if (currentBeamTime > beamTime && beamCount <= 4)
            {
                beamCount += 1;
                currentBeamTime = 0f;
            }

        }
    }

I learned I needed to use OnTriggerStay2D in order to not have to move the object for the ammo counter to go up, and I had definitely way over complicated it.