5
\$\begingroup\$

I have scene A loaded.

I load scene B with LoadLevelAdditive.

How do I unload scene B and keep scene A loaded?

\$\endgroup\$
  • \$\begingroup\$ A lot of people have requested an erase method of some sort on the forums, but haven't seen anything surface about it. \$\endgroup\$ – Ash Blue Jul 14 '14 at 5:27
7
\$\begingroup\$

You essentially need to Destroy(..) the GameObjects that came from your 2nd scene, manually.

Quick solution: for your 2nd scene, make an empty root GameObject positioned at global 0,0,0. Any other GameObject in your scene becomes a child of that. After loading the 2nd scene additively, the root will be in the hierarchy. Just Destroy(...) the root, and all GameObjects that came from the 2nd scene will be deleted too.

\$\endgroup\$
  • 2
    \$\begingroup\$ Ok, so a scene is really just a list of objects, and once they come in, it's not really known which scene they came from. Got it. \$\endgroup\$ – Almo Mar 11 '14 at 14:34
  • 1
    \$\begingroup\$ I know this is old, but just in case someone is still looking for it: I f you do use his technique of adding the whole scene as a child of an empty object, you can just name the root object with the name of the scene and then get it with GameObject.Find("name"); \$\endgroup\$ – Matheus Rocha Apr 28 '18 at 0:55
  • 1
    \$\begingroup\$ This solutions has some bug: it leaves many opened empty scenes and you'll have a garbage after some opens and closes of additive scene. \$\endgroup\$ – JavaRunner Sep 16 '18 at 4:07
9
\$\begingroup\$

Just giving an up to date answer. In 2017.1 you use UnloadSceneAsync to unload loaded levels. The tips and tricks section at the bottom of MultiSceneEditing has some guidance.

\$\endgroup\$
0
\$\begingroup\$

Usually if you want the current level to unload when the new level has finished loading, you would use LoadLevelAsync instead, as it will unload the current level once the new one is finished loading. This allows you to bring up an animated loading screen or something similar.

If you're loading in multiple levels, you may have to LoadLevelAsync on the first one, and once it's complete, load all others with LoadLevelAdditiveAsync.

If you're trying to stream in and out levels, you will probably have to create a manager to destroy the objects manually once a level needs to be unloaded.

Reference: http://answers.unity3d.com/questions/18766/how-to-unload-after-calling-loadleveladditive.html

\$\endgroup\$
  • \$\begingroup\$ This does not answer my question. How do I unload a scene that has been loaded with LoadLevelAdditive? I have already read the answer you are quoting. \$\endgroup\$ – Almo Mar 11 '14 at 14:16
  • 2
    \$\begingroup\$ I'm not upvoting because this doesn't really answer the question. However I'm not downvoting either because I personally found the information in this answer useful. \$\endgroup\$ – jhocking Mar 11 '14 at 15:03
-1
\$\begingroup\$

Applicaton.UnloadLevel("Level name");

\$\endgroup\$
  • 2
    \$\begingroup\$ Could you expand a bit on that? We generally like detailed answers :) \$\endgroup\$ – Vaillancourt Nov 26 '15 at 14:26
  • \$\begingroup\$ According to the docs, this seems to be the correct answer. \$\endgroup\$ – Seth Battin Nov 27 '15 at 0:20
-1
\$\begingroup\$

On Unity 5.6.1 (Currently using it for a project on work) I'm using: SceneManager.UnloadSceneAsync("SceneName");

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.