Unity – Programmatically Count the Scenes in Build Settings

Updated: September 22, 2021 By: Pennywise Post a comment

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!

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

Related Articles