Trying to understand StartCoroutine

Hello!

I have found old code of me and I have to admit I don’t understand it fully :confused:

Here is the code:

private IEnumerator Start() {
   yield return StartCoroutine( LoadSceneAndSetActive(startScene));

   yield return StartCoroutine( FadeOutFullscreenBrightImage(1.0f) );
}

private IEnumerator LoadSceneAndSetActive(string sceneName) {
   yield return SceneManager.LoadSceneAsync(sceneName, LoadSceneMode.Additive);
   Scene newlyLoadedScene = SceneManager.GetSceneByName(sceneName);
   SceneManager.SetActiveScene(newlyLoadedScene);
}

private IEnumerator FadeOutFullscreenBrightImage(float fadeOutTime) {
    while(condition) {
       ...
       yield return null;
    }
   yield return null;
}

Here is the stuff I don’t understand fully:

  1. Is there a difference between yield return StartCoroutine(Foo()) and just StartCoroutine(Foo())?

  2. When I call yield return StartCoroutine( LoadSceneAndSetActive(startScene)); it seems like execution is passed to LoadSceneAndSetActive() (Start is paused) so I assumed when I call yield return SceneManager.LoadSceneAsync() I assumed LoadSceneAndSetActive() is paused until LoadSceneAsync() is done. But it seems like LoadSceneAsync() immediately returns. Is this true?

  3. With this code I want to load a scene and fade out a white fullscreen quad. But it seems like the scene loading and fading is done in parallel. Isnt my code bad then? Shouldnt I WAIT for the scene loading to be finished?

Do you know anything about enumerators in C#?

  1. The manual has the answers you seek. If you yield it then it will be sequential. Unity - Scripting API: MonoBehaviour.StartCoroutine

2 &3) LoadSceneAsync() returns an AsyncOperation object. You need wait for it to finish.
There is an example in the manual: Unity - Scripting API: SceneManagement.SceneManager.LoadSceneAsync

Coroutines in a nutshell:

Splitting up larger tasks in coroutines:

Coroutines are NOT always an appropriate solution: know when to use them!