Used modulus to loop through levels

Hey guys, Ben hinted at this in the lecture so I figured I’d share my implementation of looping through levels using modulus to loop back to the first level.

private void LoadNextLevel()
    {
        int currScene = SceneManager.GetActiveScene().buildIndex;
        int nextScene = (currScene + 1) % SceneManager.sceneCountInBuildSettings;
        SceneManager.LoadScene(nextScene);
    }

If you don’t know much about modulus, think of how a clock never goes past 12. It’s more or less the same concept, but with whatever number you choose (in this case, the amount of scenes).

1 Like

Privacy & Terms