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");
}
}
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.
You get the same warning/error even if you use Application.LoadLevelAdditive or Application.LoadLevelAdditiveAsync.
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?
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.