Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 /
avatar image
0
Question by AryaMh · Aug 15, 2017 at 10:46 AM · scene-loadingloading screenloadlevelasync

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;
     }
   }
Comment
Add comment · Show 6
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 x4637x · Aug 15, 2017 at 10:52 AM 0
Share

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?

avatar image AryaMh x4637x · Aug 15, 2017 at 11:22 AM 0
Share

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.

avatar image x4637x AryaMh · Aug 15, 2017 at 11:32 AM 0
Share

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.

Show more comments
avatar image lifevisionvr · Jan 29, 2018 at 12:34 PM 0
Share

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!

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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


Comment
Add comment · 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
0

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;
     }
 }
Comment
Add comment · 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

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.

Follow this Question

Answers Answers and Comments

72 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

Related Questions

Display loading/progress indicator from Start()? 0 Answers

Particular loading screen works fine in Unity Editor, but gets stuck in the finished/built .exe version? 1 Answer

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges