SceneManager.LoadSceneAsync is only working once
So what's happening is that the LoadSceneAsync works perfectly...once. Then it fails to work the second time.
My game only has one level, so I only need to use this for the scene change between the main menu and the level scene. The first time this happens, it works perfectly, displaying the loading animation and then loading the level. However, after exiting the level and going back to the main menu and clicking the same start button, the script no longer works. The second time the loading script is called, the game switches to the loading screen, and then freezes.
Here is the script I am using: using UnityEngine; using System.Collections; using UnityEngine.UI; using UnityEngine.SceneManagement;
public class LoadingScreen : MonoBehaviour {
AsyncOperation async;
void Awake () {
}
// Use this for initialization
void Start () {
StartCoroutine("LevelSceneLoading");
}
// Update is called once per frame
void Update () {
}
IEnumerator LevelSceneLoading() {
yield return new WaitForSeconds (2);
async = SceneManager.LoadSceneAsync("Level_Scene");
while (!async.isDone) {
Debug.Log(async.progress);
yield return async;
}
}
}
The code has been revised to no longer include Destroy(gameObject); but still doesn't work.
Answer by TBruce · Oct 10, 2016 at 04:33 PM
Try removing this
Destroy(gameObject);
It unfortunately doesn't solve the issue. I actually added that after experiencing this problem, hoping it would reset the async.
It is important that you do not destroy the game object, otherwise you will not be able to continue processing in the current script or any other scripts attached to the current object (if any).
LoadSceneAsync()
does not work completely as one would suspect. IsDone
becomes true when progress
reaches 1.0. Unfortunately progress
only get to 90%. Check out the modified co-routine.
IEnumerator LevelSceneLoading() {
yield return new WaitForSeconds (2);
async = Scene$$anonymous$$anager.LoadSceneAsync("Level_Scene");
async.allowSceneActivation = false;
while ((!async.isDone) && (async.progress <= 0.9f)) {
Debug.Log(async.progress);
yield return null;
}
async.allowSceneActivation = true;
// now activate the scene
Scene$$anonymous$$anager.SetActiveScene(Scene$$anonymous$$anager.GetSceneByName("Level_Scene"));
}
Answer by archelyte_vz · Oct 10, 2016 at 05:16 PM
The issue was in the return value
This is what the corrected Coroutine looks like: using UnityEngine; using System.Collections; using UnityEngine.UI; using UnityEngine.SceneManagement;
public class LoadingScreen : MonoBehaviour {
AsyncOperation async;
IEnumerator LoadLevelRealProgress () {
yield return null;
async = SceneManager.LoadSceneAsync("Level_Scene");
StopCoroutine("LoadLevelRealProgress");
while (!async.isDone) {
Debug.Log(async.progress);
yield return async;
}
}
}
The reason why this works is because the while loop never gets processed. See my latest response above.
Because async.progress
never gets past 90%, async.isDone
will always be false and the loop is never exited.
Your answer
![](https://koobas.hobune.stream/wayback/20220612094350im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Resources.Load and dynamic loading when call to 1 Answer
Best way to serialize a big array of a class 2 Answers
Saving/loading inherited class scripts (C#) 1 Answer
BinaryFormatter - Save and Load lists of data? 0 Answers
Dynamic script loading? 0 Answers