• 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 /
  • Help Room /
avatar image
Question by unity_sq26Sg98wIfncw · Mar 27, 2020 at 07:25 PM · scene-loadingscene-switchingload scenescene loadloadlevelasync

Order of SceneManager.LoadSceneAsync and UnloadSceneAsync.

I wrote some codes using SceneManager.LoadSceneAsync()

 IEnumerator LoadTest()
 {
 //load persistent scene(some managers are placed here)
 yield return SceneManager.LoadSceneAsync("persistentScene", LoadSceneMode.Single);

 //load first scene
 yield return SceneManager.LoadSceneAsync("firstScene", LoadSceneMode.Additive);
 
 //preload second scene
 var secondScene= SceneManager.LoadSceneAsync("secondScene", LoadSceneMode.Additive);
 
 //dont activate immediately
 secondScene.allowSceneActivation = false;
 
 //instead of fadeout
 yield return new WaitForSeconds(1.0f);
 
 //Unload first scene
 SceneManager.UnloadSceneAsync("firstScene");
 
 //activate second scene
 secondScene.allowSceneActivation = true;
 }

It works like
construct "persistentScene"
↓
construct "firstScene"
↓
construct "secondScene"
↓
destruct "firstScene"

How can i destruct "firstScene" before construct "secondScene"?

Comment

People who like this

0 Show 3
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 Kastenfrosch2 · Jun 21, 2020 at 09:12 AM 0
Share

Did you solve or find a workaround for your problem? I'm currently facing the same issue and don't see any way to achieve my goal. The Unloading always waits for the loaded scene to be activated :(.

avatar image unity_sq26Sg98wIfncw Kastenfrosch2 · Jun 21, 2020 at 09:24 AM 0
Share

Yes.

 //Get current scene instance 
 var currentScene = SceneManager.GetSceneByName(currentSceneName);
 
 //preload 
 var loadAsync = SceneManager.LoadSceneAsync(_nextScene, LoadSceneMode.Additive); 
 loadAsync.allowSceneActivation = false;

 //wait for complete loading and fadeout
 while(loadAsync.progress < 0.9f || !Fade.IsFadeoutCompleted)
     yield return null;
 
 //Destroy GameObjects in CurrentScene 
 foreach (GameObject go in currentScene.GetRootGameObjects()) Destroy(go);

 //or like this
 //GameObject[] gameObjects = currentScene.GetRootGameObjects();
 //int maxDestroyPerFrame = 128;
 //for(int i = 0, int cnt = 0; i<gameObjects.length; ++i,++cnt)
 //{
 //    Destroy(gameObjects[i]);
 //    if(cnt > maxDestroyPerFrame) 
 //    cnt  = 0; yield return null;
 //}

 loadAsync.allowSceneActivation = true;
 yield return loadAsync;
 yield return SceneManager.UnloadSceneAsync(currentScene);






avatar image Kastenfrosch2 unity_sq26Sg98wIfncw · Jun 21, 2020 at 01:27 PM 0
Share

Hey, thanks for your answer. Its seems a bit hacky, because i don't get a sceneUnloaded-callback this way and I need special handling whenever someone asks "IsSceneLoaded", but I guess it's better than nothing :).

0 Replies

  • Sort: 

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

204 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Load all scenes in splash screen 0 Answers

Printing total time to open/loading a scene 0 Answers

Problem with changing scenes: Stuck in a loop where two scenes keep loading each other 0 Answers

Start function not called after reloading scene 0 Answers

multiple of same objects from DontDesroyOnLoad 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