# Why doesn't anything run after the yield return new line

I am trying to make a fade to another scene in unity when I came across a problem. For some reason when the coroutine begins the Ienumerator it only runs half of the Ienumerator Method which is only the anim.SetForBlue line. Heres my code.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class Loseforcube1 : MonoBehaviour
{

public GameObject blueWinPanel;
public Animator animForBlue;
// Use this for initialization
void Start()
{
blueWinPanel = GameObject.Find("Blue win panel");
animForBlue = blueWinPanel.GetComponent<Animator>();
}

public IEnumerator WaitTillNextScene()
{

yield return new WaitForSeconds(3.9f);
}

void OnTriggerExit()
{
if (GameObject.Find("Cube Player 2"))
{
StartCoroutine(WaitTillNextScene());

}
}
}


How have you determined that it "only runs half" of the method? You've asked it to wait 3.9 seconds, if you put a breakpoint and tried to debug, then of course the method will hit the yield statement and continue as if it never executes the rest of the function.

The problem is that execution will continue 3.9 seconds later.

For example, I have this script:

public class TestCoroutine : MonoBehaviour {
void Start() {
StartCoroutine(WaitTillNextScene());
}

public IEnumerator WaitTillNextScene() {
Debug.Log("Time before yield: "+ Time.time);
yield return new WaitForSeconds(3.9f);
Debug.Log("Time after yield:  " + Time.time);
}
}


And this is the output I get:

But there's almost four seconds between those two debug lines.

If the second scene isn't loading, your problem is likely elsewhere, for example, do you have three scenes in your build settings? Are you accounting for the delay it takes for a non-asynchronous LoadScene to load the scene? Depending on the size of the new scene, this could take much longer than 4 seconds!

• But the next scene is literally just a button and a background image and I have another script where something similar worked. – Jayson Meribe Mar 7 '18 at 17:32
• @JaysonMeribe Ok, but that doesn't actually have any bearing on why its not working. – Draco18s no longer trusts SE Mar 7 '18 at 18:07
• So is there a way I might be able to do this using the coroutine or do I have to use something else like a timer to accomplish this task. – Jayson Meribe Mar 8 '18 at 4:58
• A coroutine should work just fine, the problem is I do not know what your problem is. There is nothing wrong with the coroutine and WaitForSeconds – Draco18s no longer trusts SE Mar 8 '18 at 5:35
• Wait so you mean that after it runs the animForBlue trigger and starts the yield line after about 3.9 seconds it should load the next scene. – Jayson Meribe Mar 8 '18 at 5:50

Well, if you think about what the return keyword does, it returns, or in other words, exists the function at that specific point in execution. So that code that is below the yield return is never called, because it simply can't be reached.

If you are wanting to execute that piece of code, then putting it above the yield return is what you need to do. Hope that clarified your issue. Again, nothing can be run after the return keyword. That's most likely why it's not working for you. :)

• This would normally be true...except that that is not what actually happens. The yield keyword is used by coroutines to "pause" the execution of the contained method until "some future time." Yeilding null will wait 1 frame. Yielding something like WaitForSeconds will...wait that many seconds. – Draco18s no longer trusts SE Mar 7 '18 at 4:32
• Ah, I see. Well, I didn't exactly remember exactly how the yield return worked, since I haven't used Unity in a while. I just answered the question to the best my knowledge. Anyhow, thanks for the clarification. – dev9999 Mar 7 '18 at 5:09