0
\$\begingroup\$

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()
    {
        animForBlue.SetTrigger("FadeBlueWin");

        yield return new WaitForSeconds(3.9f);
        SceneManager.LoadScene(2);
    }



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

        }
    }
}
\$\endgroup\$

2 Answers 2

2
\$\begingroup\$

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:

After 4 seconds

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!

\$\endgroup\$
7
  • \$\begingroup\$ But the next scene is literally just a button and a background image and I have another script where something similar worked. \$\endgroup\$ Mar 7, 2018 at 17:32
  • \$\begingroup\$ @JaysonMeribe Ok, but that doesn't actually have any bearing on why its not working. \$\endgroup\$ Mar 7, 2018 at 18:07
  • \$\begingroup\$ 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. \$\endgroup\$ Mar 8, 2018 at 4:58
  • \$\begingroup\$ 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 \$\endgroup\$ Mar 8, 2018 at 5:35
  • \$\begingroup\$ 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. \$\endgroup\$ Mar 8, 2018 at 5:50
-2
\$\begingroup\$

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. :)

\$\endgroup\$
2
  • \$\begingroup\$ 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. \$\endgroup\$ Mar 7, 2018 at 4:32
  • \$\begingroup\$ 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. \$\endgroup\$
    – dev9999
    Mar 7, 2018 at 5:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .