You can just mod the next index by the total number of scenes to loop

    void LoadNextLevel() {
        int currentSceneIndex = GetCurrentSceneIndex();
        int totalScenes = SceneManager.sceneCountInBuildSettings;

        SceneManager.LoadScene(++currentSceneIndex % totalScenes);
    }

Yes, this will ‘wrap’ the scenes back to 0 when you get to the end of the list. It only works when incrementing, though. It doesn’t work when decreasing (looping backwards).

Very nice, I use this all the time

You should be able to go backwards using something like:

(currentSceneIndex - 1 + totalScenes) % totalScenes
1 Like

Yea, I just meant that

currentScene = (++currentScene % totalScenes);

will do 0, 1, 2, 0, 1, 2, but

currentScene = (--currentScene % totalScenes);

will not do 2, 1, 0, 2, 1, 0

Privacy & Terms