- Home /
How to load scenes Asynchronously?
I want to load my scenes asynchronously, I have 3 levels, when the player reaches some point the loading appears and waits till the next scene is done loading, this works only once (going from level 1 to 2). But it doesn't work properly when switching from level 2 to level 3.
IEnumerator LoadLevelAsynchronously(int sceneIndex)
{
AsyncOperation operation = SceneManager.LoadSceneAsync(sceneIndex);
operation.allowSceneActivation = false;
while (!operation.isDone)
{
if(operation.progress >= 0.9f)
{
operation.allowSceneActivation = true;
}
yield return null;
}
}
Have you check your sceneIndex is correct when start this coroutine? Also check the game object have this script on is set to don't destroy on load?
yes I have, everything is fine. But the problem arises only when I'm loading scenes one after another, i.e. level1 -> level2 -> level3. When I load like only from level(i) to level(i+1) (only two scenes), it works fine.
You might want to add some debug message at the start of your coroutine to check if it is actually being call. I think most likely reason that cause this problem is loss your script during level1 -> level2. Which is why i ask have you set "Don't destroy on load" to the game object that has this script attached. If this is not the case I might need more information in order to help you figure it out.
Could you copy the entire code in to the answer Arya$$anonymous$$h. I am also have trouble getting scenes to load async, and if you have it so that it loads from scene one to 2 then I would like to give it a try. If you would share that I would greatly appreciate it!
Answer by uvavoo · Aug 15, 2017 at 12:52 PM
I assume the script is attached to a game object? This game object is destroyed when the new level loads. Add this script and I think this should solve the problem.
using UnityEngine;
using System.Collections;
public class DontDestroy : MonoBehaviour {
// Use this for initialization
void Awake () {
DontDestroyOnLoad(gameObject);
}
}
Answer by kennenwe · Jan 29, 2018 at 07:16 PM
I dont know if your code is activated trough trigger or button but mine is trough button. Here is the code:
//Reference to the loading screen public GameObject LoadingScreen; //Reference to the loading bar public Slider slider;
//The button function
public void LoadLevel(int sceneIndex)
{
StartCoroutine(LoadAsynchronously(sceneIndex));
}
IEnumerator LoadAsynchronously(int sceneIndex)
{
AsyncOperation operation = SceneManager.LoadSceneAsync(sceneIndex);
//activate the load screen
LoadingScreen.SetActive(true);
while (!operation.isDone)
{
float progress = Mathf.Clamp01(operation.progress / 9f);
//Set the slider value = to the progress
slider.value = progress;
yield return null;
}
}
Your answer
Follow this Question
Related Questions
Display loading/progress indicator from Start()? 0 Answers
Is there a perferred method for handling scene loading? 1 Answer
How to wait for baked lighting to load when using LoadSceneAsync? 0 Answers
SceneManager.LoadSceneAsync causes lag spikes, how to make background loading less CPU heavy 1 Answer