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
1
Question by biscuitquiff_r · Nov 07, 2017 at 04:42 PM · coroutinecoroutinesloadlevelasync

Code not executed after LoadSceneAsync in a Coroutine

I'm relatively new to Coroutines and Level Loading, so please forgive my presumably rookie mistake.

I'm calling the following function using StartCoroutine:

 public IEnumerator LoadLevel(string levelPath)
         {
             AsyncOperation loadOp = SceneManager.LoadSceneAsync(levelPath);
             while (!loadOp.isDone)
             {
                 print("loading...");
                 yield return null;
             }            
     
             print("loaded level");
 }


The level loads, "loading..." is printed once, but the second print never occurs. What have I missed here, or not understood? I thought execution would carry on after the while loop was exited?


Thanks for the assistance!

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Bunny83 · Nov 07, 2017 at 05:01 PM

Once the new scene is loaded and activated the old scene is destroyed at the same time. That means unless you have used DontDestroyOnLoad, the object that runs the coroutine will also be destroyed. This will terminate the coroutine as well.


So there are basically two ways:

  • Run your coroutine on a top level gameobject in the old scene and use DontDestroyOnLoad on that gameobject in order to make it "survive" the level load.

  • Another way would be to set allowSceneActivation to false. However in this case you can't wait for "isDone" to turn true as this will never happen as you can read in the documenation. Instead you should check the progress if it reached "0.9". Now you can do some actions before the actual scene switch. However when you set it back to true and the actual level switch happens your old object will be destroyed and the coroutine is gone.

It depends on what you actually want to do.

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 biscuitquiff_r · Nov 07, 2017 at 09:16 PM 0
Share

Thanks, yeah, it's been set up on a game object that's set to be kept after a level load; and the gameobject is still there in the scene after loading is complete; which is why I'm perplexed as to why it's not continuing...!

avatar image biscuitquiff_r biscuitquiff_r · Nov 08, 2017 at 08:28 AM 0
Share

Tried different variations of this with no luck:

 public IEnumerator LoadLevel(string levelPath)
 {        
         AsyncOperation loadOp = Scene$$anonymous$$anager.LoadSceneAsync(levelPath);
         loadOp.allowSceneActivation = false;
 
         while (loadOp.progress < 0.9f)
         {
             print("loading..." + loadOp.progress);
             yield return null;
         }            
 
         print("mostly loaded level");
         loadOp.allowSceneActivation = true;
 }

Never reaches "mostly loaded level" print, and therefore never activates the scene either. Same issue as using .isDone...

Do Coroutines get killed when a level is loaded?!

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

77 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

Related Questions

Coroutines IEnumerator not working as expected 2 Answers

Coroutine is jerky after exiting trigger zone 1 Answer

Wait for coroutine to finish before returning a value in another function 0 Answers

Wait bunch of coroutines inside of coroutine 3 Answers

understanding coroutines 0 Answers


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