- Home /
SceneManager.LoadSceneAsync stops coroutine
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.
Can you add some comment to your code to let us know which line of code working and which line being skipped?
Thanks for the quick reply. I've updated the code.
hmm, does any error display on your console log?I wondering that that your player gameobject has been destroyed when new scene loaded.
Answer by Obsessi0n · Jan 31, 2018 at 01:16 AM
When the scene is loaded the script is destroyed you need to make it to not get destroyed during scene change.
public class ExampleClass : MonoBehaviour {
void Awake() {
DontDestroyOnLoad(transform.gameObject);
}
}
Answer by karmington · May 09, 2018 at 07:49 PM
Coroutines just die quiet deaths on scene changes, doesnt help if they are called out of DontDestroy objects.
Answer by scottpurcival · Nov 18, 2020 at 02:09 PM
Sorry for the necro. I finally figured this out, and could not find the answer anywhere online. Posting for others.
Your AsyncOperation load is being set to null. For some reason unity does not report this as a null reference error in the logger, it just quits the coroutine quietly.
Do this; while (load != null && !load.isDone) { yield return null; }
Your answer
![](https://koobas.hobune.stream/wayback/20220612150201im_/https://answers.unity.com/themes/thub/images/avi.jpg)