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 Xarbrough · Jul 29, 2016 at 10:10 PM · c#load sceneasyncadditive

LoadSceneAsync additive and moving scene root problem

I am loading multiple scenes into a main scene at run-time. Each new scene has a single root object, which I then want to position in the main scene's world space.

My code has a small but annoying problem: I load a new scene, it then shows up at the center of the main scene. Then, after one frame, my positioning code is run and moves the new object to the correct position, resulting in a one frame glitch at every load.

 IEnumerator LoadDungeonScene(string sceneName)
 {
     Scene activeScene = SceneManager.GetActiveScene();
     AsyncOperation loadOperation = SceneManager.LoadSceneAsync(sceneName, LoadSceneMode.Additive);
 
     // Wait until the scene has loaded. Once is isDone, it actually shows up at origin,
     // but I still wait another frame, which I don't want, since it shows as a visual glitch.
     while (loadOperation.isDone == false)
         yield return new WaitForEndOfFrame();
 
     // Scene has loaded, get reference to root GameObject.
     Scene loadedScene = SceneManager.GetSceneByName(sceneName);
     Transform root = loadedScene.GetRootGameObjects()[0].transform;
 
     // This position assignment is run 1 frame after the scene was loaded and showed up at origin.
     root.position = new Vector3(100f, 200f, 300f);
 
     // Do I need this? Why would I want to merge scenes at runtime?
     SceneManager.MergeScenes(loadedScene, activeScene);
     yield return null;
 }

Is there any better way to load a scene, position some objects and then show them? My hacky solution might be to deactivate the root object in every scene before saving, but that would be pretty annoying as well, since there are around 20 scenes and we have several people working on them.

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
Best Answer

Answer by ScaniX · Jul 29, 2016 at 11:03 PM

Probably your wait loop is the problem. Try using yield return null instead of the yield return WaitForEndOfFrame().

I also have an additional waiting loop after waiting for the operation, which I added sometime during development, but I am unsure if that is still necessary.

 while (!scene.isLoaded)
     yield return null;

I am doing a repositioning as well when loading the new scene (although not additive and I reposition the player, and not the scene) and I don't have a bad frame inbetween.

Maybe also check this: http://answers.unity3d.com/questions/755196/yield-return-null-vs-yield-return-waitforendoffram.html

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 Xarbrough · Jul 29, 2016 at 11:30 PM 1
Share

Indeed, the problem was that WaitForEndOfFrame waited until the end of the frame, but the Async operation could finish loading anywhere in between and show visually for the remaining fraction of the frame. Yield return null returns immediately and works correctly now. Thank you!

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

197 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Loading Bar in Different Scenes 1 Answer

Feature 'async function' is not available in C# 4 2 Answers

New Input System Doesn't Work After Load Scene 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