private void LoadNextLevel()
{
int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
int nextSceneIndex = SceneManager.sceneCountInBuildSettings - 1; // todo the right thing
if (currentSceneIndex < nextSceneIndex)
{
SceneManager.LoadScene(nextSceneIndex);
}
else
{
LoadFirstLevel();
}
}
Hi Regnian,
If we work through what will happen…
Let’s assume there are a total of 10 scenes in the game and that we are currently on the first scene, just about to win, thus;
currentSceneIndex = 0
and nextSceneIndex = 8
The if
statement asks, is the current scene index less than the next scene index, as we can see from the numbers above, this is going to be true.
The next line loads the scene, using the value of 8, thus taking you to the last scene, missing all those in between.
At the end of this level, currentSceneIndex = 8
and nextSceneIndex = 8
, the condition for the if
statement will not be true this time, as the two values are equal, thus the else
code block will be run. Your first level is now loaded.
Repeat.
Hope this helps
Rob,
That makes the code written in the tutorial make a load more sense. I hadn’t considered or thought of, what would happen in a case like that. Fantastic! Thank you for taking the time to map that out, when I come back to edit this game for aesthetics and more levels that will save me quite a bit of a possible headache.
– Anthony
Hi Anthony,
You’re welcome, it can be really useful to walk/step through things like this.