Coroutine Ends at Yield

Hi Guys,

I’m trying to get a Coroutine to do a simple yield WaitForSeconds, but I’ve found that calling any yield in the IEnumerator ends the script, and I’m not sure why.

    bool FireShot(Vector3 firePoint, int shotsFired)
    {
        if (firePoint == default(Vector3))
            Debug.Log("FireHandler::FireShot- Firepoint is null!!");


        StartCoroutine(WeaponFired(firePoint, shotsFired));
        return true;
    }

    IEnumerator WeaponFired(Vector3 hitLocation, int rounds)
    {
        // Uncommenting this yield directly below means that nothing in this function is ever called!
        //yield return null;
        gwData.SetCurrentView("Binocular");
        gwData.SetActiveView();
        float randRadius = explosionRadius * 0.5f;
       
        if(bHitStatus = CheckForHit(hitLocation))
        {
            Instantiate(hitEffect, hitLocation, Quaternion.identity);
        }
        else
        {
            Instantiate(missEffect, hitLocation, Quaternion.identity);
        }
        // Activating this means EVERYHING below it is never called
        //yield return new WaitForSeconds(3);
        Debug.Log("FirstCheck (this will not be called EVER if I uncomment the above yield)");
        if(rounds > 1)
        {
            for (int i = 0; i < rounds; i++)
            {

                Vector3 newRandomisedPos = new Vector3((hitLocation.x + Random.Range(-randRadius, randRadius)), hitLocation.y, (hitLocation.z + Random.Range(-randRadius, randRadius)));

                bool bRemoveStatus = false;
                if (bHitStatus)
                {
                    bRemoveStatus = bHitStatus;
                }
                if (bHitStatus = CheckForHit(newRandomisedPos))
                {
                    Instantiate(hitEffect, newRandomisedPos, Quaternion.identity);
                    if(bRemoveStatus)
                    {
                        bHitStatus = false;
                    }
                }
                else
                {
                    Instantiate(missEffect, newRandomisedPos, Quaternion.identity);
                }
               // It never actually does a second for loop, even though upon checking the script using breakpoints, it's saying the rounds == 3
                yield return new WaitForSeconds(3);
            }
        }
    }

I’ve used Coroutines before and never experienced this. I’m pretty sure they’re not supposed to work like this. There is no script anywhere cancelling this Coroutine. Can anyone see the issue?

Thanks!

Have you tried putting debug.log right before and after the yield to confirm that this is the place it breaks?

Got it lol. Thanks :slight_smile:

What was the problem?

… gwData.SetCurrentView(“Binocular”) was disabling the parent.parent object this scripts attached to lol.