• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by archelyte_vz · Oct 10, 2016 at 04:22 PM · c#loading

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by TBruce · Oct 10, 2016 at 04:33 PM

Try removing this

 Destroy(gameObject);
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image archelyte_vz · Oct 10, 2016 at 04:36 PM 0
Share

It unfortunately doesn't solve the issue. I actually added that after experiencing this problem, hoping it would reset the async.

avatar image TBruce archelyte_vz · Oct 10, 2016 at 06:28 PM 1
Share

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")); 
 }

avatar image
0

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;
     }
 }

}

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TBruce · Oct 10, 2016 at 06:50 PM 1
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

246 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Why does Resources.load() only work once? 0 Answers

Waiting for Saving/Loading? 2 Answers

Saving and loading an int variable on Android 1 Answer

Spawning loot only the first time a level is loaded 1 Answer

Load score 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges