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 /
  • Help Room /
avatar image
0
Question by TheWoulfe · Sep 14, 2015 at 09:51 AM · scenescene-loadingasyncunloadloadleveladditive

Correct use of Application.UnloadLevel ?

I'm trying to use the newly added Application.UnloadLevel to unload an existing scene after loading a new one in additively in Unity 5.2 but keep getting the error:

Unloading the first loaded scene Temp/__EditModeScene (index -1), is currently not supported UnityEngine.Application:UnloadLevel(Int32)

I can't seem to find much on the correct use of this method, not even the docs give an example piece of code so I was wondering if someone could take a look at my code and see if there's anything obvious I am doing wrong.

Cheers

 public class SceneSwitcher : MonoBehaviour {
 
     private void Awake()
     {
         DontDestroyOnLoad(gameObject);
     }
 
     private void Update()
     {
         if (Input.GetKeyDown(KeyCode.L))
         {
             StartCoroutine(SwitchScene());
         }
     }
 
     private IEnumerator SwitchScene()
     {
         //load new scene
         AsyncOperation loadNewScene = Application.LoadLevelAdditiveAsync(1);
 
         while (!loadNewScene.isDone)
         {
             yield return new WaitForEndOfFrame();
         }
 
         print("Scene Loaded");
 
         //unload current/old scene
         bool unloaded = Application.UnloadLevel(0);
 
         while (!unloaded)
         {
             yield return new WaitForEndOfFrame();
         }
 
         print("Scene Unloaded");
 
         //unload the unused assets/lightmaps
         AsyncOperation unloadCurrentSceneAssets = Resources.UnloadUnusedAssets();
 
         while (!unloadCurrentSceneAssets.isDone)
         {
             yield return new WaitForEndOfFrame();
         }
 
         print("Unused Assets Removed");
     }
 }

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
0

Answer by frost4285 · Sep 24, 2015 at 12:20 PM

@TheWoulfe You only can unload levels that was loaded additively (Application.LoadLevelAdditive or Application.LoadLevelAdditiveAsync).

And error means you trying to unload level that was loaded by Application.LoadLevel or Application.LoadLevelAsync.

So Maybe in first scene you should maybe just instantiate character (or something that you will not unload) and than additively load environment.

Comment
Add comment · Show 4 · 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 TruffelsAndOranges · Sep 27, 2015 at 01:10 PM 0
Share

You get the same warning/error even if you use Application.LoadLevelAdditive or Application.LoadLevelAdditiveAsync.

avatar image frost4285 TruffelsAndOranges · Sep 27, 2015 at 04:40 PM 0
Share

Can you give an example of what you are trying to implement? In my game there is a main scene which has a level selection menu (is always loaded). Scenes (levels) are loading by Application.LoadLevelAdditiveAsync and unloading by Application.UnloadLevel when level is finished. And it works just fine. $$anonymous$$aybe you are trying to unload the first scene?

avatar image TruffelsAndOranges frost4285 · Sep 28, 2015 at 12:52 PM 0
Share

I'm not trying to unload the first scene, but I'm trying to unload the __Edit$$anonymous$$odeScene (index -1): but only after I loaded it LoadLevelAdditiveAsync, which means that it should have an index larger than -1 (but it doesn't, because of undocumented behaviour).

What I want to do is, if the level designer is editing a specific scene, and hits play, then I want that scene to be able to unload no matter what - because the level designer could be anywhere in the world, right?

The solution I came up with is to destroy all GameObjects in the scene when the game is loaded, and then reload the scene with LoadLevelAdditiveAsync to enable unloading. But this doesn't work, because that same error kinda fucks it up. So I'm left with a scene that can't be unloaded, no matter what I do.

The level designer shouldn't have to leave his scene, load a "main scene", and then click play to test his/her changes.

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

28 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

Related Questions

How do I preload multiple scenes? 0 Answers

How to Reload a Scene but keeping others already Loaded Scenes?,How to Reaload a Scene and keep others Loaded Scenes without unload then? 2 Answers

How Can I Open Previous Scene? 1 Answer

Scene Editor Problem @username 0 Answers

Is there a way to get Something from an inactive scene that's already loaded? 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