Hello,
im relativly new to unity and i got the BASICS. Now i want to try to load the next scene, but if it doesn’t exists
i want to open a different scene. I tried with try catch but that doesn’t seem to work.
Sorry for my spelling im actual german
Why wouldn’t the scene exist?
You could choose scenes by index number.
Yes, because i do it like level01, level02 i want to always go to the next level and of there no More i want to load a menu scene. I don‘t want to change an number/ variable for every Update
So if the menu is buildIndex 0, use an incremental counter that resets to 0 after reaching max length of scenes.length.
heres an eg code, will not work just as is, you will have to integrate, or rewrite around your situation. But this is the idea.
public IEnumerator LoadNextLevel()
{
if (heroCam != null)
heroCam.enabled = false;
Scene currentScene = SceneManager.GetActiveScene();
int index = currentScene.buildIndex;
index++;
if (index > scenes.Length - 1)
index = 0;
AsyncOperation asyncLoad = SceneManager.LoadSceneAsync(index, LoadSceneMode.Additive);
while (!asyncLoad.isDone)
{
yield return null;
}
foreach (GameObject g in persistentObjects)
{
SceneManager.MoveGameObjectToScene(g, SceneManager.GetSceneByName(scenes[index]));
g.transform.position = new Vector3(0, 0, g.transform.position.z);
}
asyncLoad = SceneManager.UnloadSceneAsync(currentScene);
while (!asyncLoad.isDone)
{
yield return null;
}
if (heroCam != null)
heroCam.enabled = true;
}
Strip out the heroCam and persistentObjects code. Then use it with StartCoroutine().