SceneManager.LoadScene((currentSceneIndex + 1) % 3);
is a nice way to avoid the new methode
Coming from a programming background - I automatically did that XD
This is clever, but you’re using “magic numbers.” Where does the 3 come from? (obviously, it’s the number of scenes, but…) If you add a new scene to the game, it will be unreachable in this case! Better if you can pull the total number of scenes somehow and mod that:
numScenes = GetNumberOfScenes() // this is a made up function, it probably won't work
SceneManager.LoadScene((currentSceneIndex + 1) % numScenes)
That is more scalable when dealing with manipulating more than a set number of scenes. Unity has two methods which can grab total scenes from the build, so what you listed is basically correct. They had an older method, but it’s deprecated so isn’t usable anymore.
The new method for it is
int numberOfScenes = SceneManager.sceneCountInBuildSettings;
For what its worth I arrived at the below when taking on the challenge.
Simply checking if the current scene index = the max build scene count, if it is then go back to the first scene, otherwise iterate to the next.
public void LoadNextScene()
{
int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
int sceneCountint = SceneManager.sceneCountInBuildSettings;
Debug.Log(sceneCountint);
if(currentSceneIndex == sceneCountint-1)
{
SceneManager.LoadScene(0);
}
else
{
SceneManager.LoadScene(currentSceneIndex + 1);
}
}
Pros: Single method so you dont need to worry re-defining the On Click() event if you do introduce a different final scene.
Cons: Not as transparent as having distinct methods for the reader.
Another issue if you have loading screens (which you will for larger games), is this block of code would break fairly quickly. That’s because loading screens mess up the order of scenes. However this certainly works for the games we’re working on.