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 Tinho205 · Sep 24, 2021 at 04:40 PM · scene-loadingscenesscene load

Scene.isLoaded not working properly.

So I have a very simple and small code that should do the following: Load Scene; After loaded, do stuff.
For making sure the scene would have enough time for loading, I called the LoadScene() function and made a loop under it that executes a million times (not kidding lol) checking if the scene has loaded. It never returns true. The code:

 if (Input.GetMouseButtonDown(1))
 {
   SceneManager.LoadScene(sceneToLoad); //previously defined scene name (string)
   for (int i = 0; i < 1000000; i++)
   {
     if (SceneManager.GetSceneByName(sceneToLoad).isLoaded)
     {
       //do stuff
       break;
     }
   }
 }

Apart from the one million runs loop, what's wrong with my code?

Comment
Add comment · Show 2
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 Tinho205 · Sep 24, 2021 at 08:56 PM 0
Share

I just can't continue my project without this, if anyone who reads this has absolutely ANY idea, even if it probably doesn't work, post it here. Thanks.

avatar image Tinho205 · Sep 24, 2021 at 09:18 PM 0
Share

Also, how can I formally wait until a function ends instead of having to make a loop that runs at the same time as the function?

1 Reply

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

Answer by Pangamini · Sep 24, 2021 at 09:25 PM

That loop does not cause any waiting, as it's executed synchronously. So the loading probably doesn't even start before you ask if it's done. Also you break the loop during the first execution. The correct way would be to check if the scene is loaded, then actually wait at least one frame before checking again. Check out coroutines or c# async/await pattern, those might help.

Comment
Add comment · Show 10 · 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 Tinho205 · Sep 24, 2021 at 09:53 PM 0
Share

Tysm for the answer. The break is inside the if statement, wich always returns false. The loop never breaks. About executing things synchronously, what does that mean? I'm not any kind of programmer. Does that mean it runs on the same frame?

avatar image Tinho205 · Sep 24, 2021 at 09:54 PM 0
Share

I'll check the coroutines right now.

avatar image Pangamini Tinho205 · Sep 24, 2021 at 09:56 PM 0
Share

Well, the frame won't end before that method's call ends, so yes. You are right about the break, I missed that :-O

avatar image Tinho205 Pangamini · Sep 24, 2021 at 09:59 PM 0
Share

Ok, good to know about how frames deal with big loops, ty :)

Show more comments

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

131 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

Related Questions

Load Scene from .unity File 1 Answer

One Location Game (with Scenes Loaded and Disabled) 0 Answers

Loading Specific Scenes Post-Build 0 Answers

Multiple scenes 2 Answers

Adding to delegate with multiple instances of the same object 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