Execute function after Coroutine

Hi guys,

I’m just getting a handle on CoRoutines and I can’t seem to get this to work: I am trying to have a coroutine run for 3 seconds which works, but then I want it to call subSpawn(). I’m not sure how to work around this, please help.

IEnumerator StaringTimer(float num, bool ray){
        timerStarted = true;
        for (int i = 0; i <= num; i++){
            if (rayScript.rayHit == ray){
                yield return new WaitForSeconds(1);
                print ("waited" + i);
            }
            else{
                print(ray);
                print("fail");
                timerStarted = false;
                return false;
            }
        }
        SubSpawn(ray);
        print("alldoneher");
        timerStarted = false;
    }

public void SubSpawn(bool rayHit){
        if (rayHit == true){
            newSubtitle = (GameObject) Instantiate(subtitlePrefab, player.transform.position, player.transform.rotation);
            print ("Intant");
            layerScript = newSubtitle.GetComponent<SubtitleLayers>();
            StartCoroutine(newSubtitle.GetComponent<FadeSubtitle>().coroutOpaq);
            print ("corot opaq");
        }
        else{
            StartCoroutine(newSubtitle.GetComponent<FadeSubtitle>().coroutTrans);
            print ("FadeDown");
        }
    }

Thank you in advance

At end you need to break corutine, try at end of rutine set break; or yield return;

I guess the else is being fired and its returning false. If you remove the return false, does subspawn get fired?

1 Like

@phoda You don’t need to break or exit coroutines at the end- getting to the end will automatically stop it, just like any normal null-return function. Besides, that wouldn’t explain why the SubSpawn function isn’t being called. I agree with HiddenMonk that the rayHit variable is probably changing as it approaches the 3 second mark and is forcing the “return false;” to fire, which exits the coroutine completely.

Alternatively, if the debugs aren’t being called for that, you’re forcing StopCoroutine from somewhere outside of this script, or disabling the script, or disabling the GameObject this script is on, or destroying either the script or the GameObject, or setting Time.timeScale to zero or close-to-zero somewhere.