In Unity, you can count the scenes that were added to Build Settings by using the following C# code:
using UnityEngine.SceneManagement;
/* ... */
int totalScenes = SceneManager.sceneCountInBuildSettings;
Example
The function below will load the next scene when the index of the next scene is less than the total number of scenes (we assume that each scene is a level):
private void NextScene()
{
// Get the index of the current scene
int sceneIndex = SceneManager.GetActiveScene().buildIndex;
int nextSceneIndex = sceneIndex + 1;
if (nextSceneIndex < SceneManager.sceneCountInBuildSettings)
{
SceneManager.LoadScene(nextSceneIndex);
} else {
SceneManager.LoadScene(0);
}
}
Happy coding!