- Home /
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.
Answer by ajewell503 · May 13, 2020 at 04:55 AM
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.
Answer by sacredgeometry · May 10, 2020 at 02:59 PM
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.
Thank you for your help, after sleeping on it, this is definitely too complicated. I will be trying a while loop to see if that fixes this. I would try to give a shot at a different design pattern that you have suggested, but I am struggling to understand what you are trying to suggest. Sorry about that, I would like to note that I have very little experience coding and using unity. I tried to get started on a game but realized that I didn't know how to do much. So I joined a game jam that is a week long so I can develop skills. So this will hopefully be my first complete game.
Answer by JonnyHilly · May 10, 2020 at 03:39 PM
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;
}
Your answer
Follow this Question
Related Questions
For loop & Foreach don't complete inside IEnumerator when using IndexOf 2 Answers
How to make loop with call to IEnumerator actually pause? 2 Answers
Updating waitforseconds in IEnumerator 1 Answer
Call a new IEnumerator 2 Answers
Spawn Objects Inside IEnumerator In Serverside (Unity Networking) 1 Answer