Stop for loop in IEnumerator

guys,Im new and get and trouble that cant break it out.
I use an IEnumerator with 2 loops

  IEnumerator MultyShoot()
    {
        for (int i = 0; i < firePoints.Length; i++)
        {
                ObjectPoolFx bulletRocketPoolScr = GameObject.Find("PoolRocketBullet").GetComponent<ObjectPoolFx>();
                for (int j = 0; j < bulletRocketPoolScr.poolSize; j++)
                {  
                    if ((bulletRocketPoolScr.objs_[j].activeInHierarchy) == false)
                    {
                        if (target != null)
                        {
                            bulletRocketPoolScr.objs_[j].SetActive(true);// i wana break out this loop when i set this poolObject is true
                            .......
                            }
                        }
                        break;
                }
            }   yield return new WaitForSeconds(.3f);
}
}

thanks to your help

put:

yield break;

Wherever you want to stop the iterator function (coroutine).

So just after the line where you have that comment, but the break.

That should be yield break; not yield return break;

sorry, yeah, omit the return

yield break will out IEnumerator not the loop inside, example:
for (int i = 0; i < firePoints.Length; i++) if i got 3 firePoints i should active 3 poolObjects in for (int j = 0; j < bulletRocketPoolScr.poolSize; j++)
if using yield break; only active single poolObject.

Goto is another possibility for breaking out of deeply nested loops. Use with extreme caution.

Holy cheese! Goto actually exists in C# !?

1 Like

I was shocked too. Totally horrified. But yes, it exists. For two reasons.

  • To allow fall through in switch statements
  • To break out of deeply nested loops

https://msdn.microsoft.com/en-AU/library/13940fs2.aspx

@Kiwasi
I’ve been working with C# for about 5 years and I’ve never saw a single goto. I hope I’d keep not seeing it.

You could use a simple bool something like this:

IEnumerator MultyShoot()
    {
        bool breakLoop = false; //set it to false so it doesnt break the loop first time
        for (int i = 0; i < firePoints.Length; i++)
        {
            ObjectPoolFx bulletRocketPoolScr = GameObject.Find("PoolRocketBullet").GetComponent<ObjectPoolFx>();
            for (int j = 0; j < bulletRocketPoolScr.poolSize; j++)
            {
                if ((bulletRocketPoolScr.objs_[j].activeInHierarchy) == false)
                {
                    if (target != null)
                    {
                        bulletRocketPoolScr.objs_[j].SetActive(true);// i wana break out this loop when i set this poolObject is true
                            .......
                            }
                }
                breakLoop = true; //if you should break the loop set it to true
                break; //then break the inner loop
            }
            if (breakLoop) //checks if we should break the loop
            {
                breakLoop = false; //sets it to false as we already know to break the loop
                break; //breaks the loop
            }
        }
        yield return new WaitForSeconds(.3f);
    }
1 Like

i done it in different way. Using a function to active bullet in pool make it return gameObject, so i no need to use the second loop. But I will consider @RavenOfCode 's solution, thanks guys

1 Like

Oh my lord thank you for the solution. I’ve been thinking of why it’s crashing when I enter the playmode and discovered it was the while loop. Peace.