I am writing a script that controls a door trigger. When OnTriggerEnter is called, a coroutine is started to set some variables and switch the scene with SceneManager.LoadSceneAsync. When the scene is done loading, it becomes active, however the rest of the coroutine does not finish. It just immediately ends. I am not sure if this is the way the method is supposed to act, or if I am doing something wrong. If it is the way the method works, what should I do instead to get the result I want?
This is the code that is having problems:
public IEnumerator FadeToScene(int scene, Vector2 startLoc) { PlayerController player = PlayerController.thePlayer; CameraFX cam = player.GetComponentInChildren(); player.canMove = false; player.rb.velocity *= 0; yield return StartCoroutine(cam.fadeTo(Color.black, .25F)); Time.timeScale = 0; ` ` //Everything up until this point works. Below is where problems start AsyncOperation load = SceneManager.LoadSceneAsync(scene); load.allowSceneActivation = true; while (!load.isDone) { yield return null; } //Everything below is skipped player.rb.position = startLoc; Time.timeScale = 1; yield return StartCoroutine(cam.unFade(.25F)); player.canMove = true; }
The code is in a script that is not destroyed between scenes. I have also tried moving the async load to a separate coroutine. After the scene is finished loading, the method returns to the line where it was called from, and the rest of the lines are skipped.
I am guessing that this is on purpose and I am unaware, but I am not sure.