Loading level

To load the next level and reload the current level upon failure, I turned the sceneIndex into a parameter that reads the level index every time.

private void loadLevel(int sceneIndex)
{

    Scene scene = SceneManager.GetActiveScene();

    currentSceneIndex = scene.buildIndex;

    SceneManager.LoadSceneAsync(sceneIndex); //Async is preferable, needs an index int

}

Is this ok?

This will only work if this script is on a game object that persists across scenes. If not, the value will be lost as soon as the new scene loads. Also, each time you call this method it will do this. So it will always store the previous scene’s index. if you are on scene 1 and go to scene 2, it will store 1. If you are on scene 9 and go to scene 10, it will store 9 - overwriting what was in there before.

Privacy & Terms