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.