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");
}
}