Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 ss · Sep 11, 2013 at 10:49 PM · coroutineloadleveladditiveasync

LoadLevel*Aynsc() and Coroutine

I have a LoadScene() method within my script which looks like this,

 void LoadScene(String scene)
 {
     async = Application.LoadLevelAdditiveAsync(scene);
     StartCoroutine(SceneLoadCallback(scene));
 }

where 'async' is declared as follows,

 private static AsyncOperation async = null;

The Coroutine 'SceneLoadCallBack()' is defined as follows,

 IEnumerator SceneLoadCallback(String scene)
 {
     while (!async.isDone) {
         Debug.Log("Checkpoint-1")
         yield return null;
         Debug.Log("Checkpoint-2")
     }
     
     yield break;
     Debug.Log("Checkpoint-3")
 }

The method 'LoadScene()' is called directly from the 'Update()' method of my script. The level that I am trying to load gets loaded correctly, however 'Checkpoint-2' and 'Checkpoint-3' never get printed.

I am new to Unity and have just learned how to use Coroutines. If I understand it right, 'yield return null' should cause Coroutine body to resume executing (after Update() is called) at where it left. But that doesn't seems to be happening. Any insights here would be helpful.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by dorpeleg · Sep 12, 2013 at 07:06 AM

You should run the method 'LoadScene()' from 'Start()' and not from 'Update()'.

Coroutines do not need to be in 'Update' in order to run.

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 raimon.massanet · Sep 12, 2013 at 07:40 AM

We experienced a similar issue whith LoadLevelAsync. It seems the isDone property does not work as expected and is never set to true (unless it has been fixed in recent releases). Instead we used the progress property and checked when it was over 0.91f. I know it's not a very ellegant solution, but it works.

Another approach you might want to try is to set the allowSceneActivation to false after calling LoadLevelAsync, then do whatever you want to do while the scene is loading, and set allowSceneActivation back to true when you have finished. That way you don't need to control when the scene has finished loading because it will automatically load when it's ready.

Finally, yield break, breaks the flow of the coroutine, which means that anything after that line will never get executed.

Comment
Add comment · Show 9 · 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 dorpeleg · Sep 12, 2013 at 10:07 AM 0
Share

From my experience, progress also doesn't work as expected.

Unless you use them both:

 if (!async.isDone) {
     Debug.Log(async.progress);
 }

It's a known bug (at least for users, not sure if unity is aware).

avatar image raimon.massanet · Sep 12, 2013 at 10:11 AM 0
Share

We used the progress property as described above (check if progress is above 0.89f, I wrote it wrong before) and the solution worked for us. It seems as if the last 0.1f is never completed unless the scene is allowed to load.

avatar image dorpeleg · Sep 12, 2013 at 12:45 PM 0
Share

It seems as if the last 0.1f is never completed unless the scene is allowed to load.

Interesting remark... will look out for that.

avatar image raimon.massanet · Sep 12, 2013 at 01:28 PM 0
Share

Please let us know what you find out :-D

avatar image ss · Sep 12, 2013 at 02:31 PM 0
Share

Based on the answer by raimon.massanet, I have now changed my SceneLoadCallBack() method to look as follows,

 IEnumerator SceneLoadCallback(String scene)
     {
         while (!(async.progress>0.89f)) {
             Debug.Log("Progress: "+async.progress);
             yield return null;
         }

         Debug.Log("Checkpoint-3");             
         yield break;
     }

Still, the 'Checkpoint-3' statement never gets printed. The progress statement gets printed just once with a value of 0.

I have also tried setting a breakpoint inside the while loop, on the yield line, and when it gets executed (just once) the watch on the 'async' variable says '$$anonymous$$ identifier'. I am not sure if that has anything to do with the behavior I am seeing, but I cannot explain why that may be happening as 'async' is a static variable.

Show more comments
avatar image
0

Answer by Weendie-Games · Jan 17, 2018 at 11:10 AM

"AsyncOperation.isDone will never be true if AsyncOperation.allowSceneActivation is false. Progress will stop at around 0.9f."

Check this answer: https://answers.unity.com/answers/956839/view.html

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

18 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

Related Questions

Can I use WWW in Start()? 1 Answer

Coroutine not working properly on Android (C#) 0 Answers

Problem detection collider with coroutine 1 Answer

Get static var inside coroutine (C#) 1 Answer

Sequential Coroutines sometimes halt 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