SceneManager not finding scene at buildIndex, even though there is one.

I have a “level advancement” system, and I am trying to load the level with the buildIndex of the current level plus 1 (the next level). The SceneManager throws IndexOutOfRangeException: Scene index "1" is out of range.at the SceneManager.GetSceneAt line whenever I attempt to load the scene. There is a scene at buildIndex 1 in the Build Settings.

Code:

if (playerFinishedLevel && !playerDead) { //win
			disablePlayerPhysicsAndControls();
			if (Input.GetKey(KeyCode.Space)) { //trigger load
				Scene sceneToLoad = SceneManager.GetSceneAt(SceneManager.GetActiveScene().buildIndex + 1);
				Debug.Log("Loading level \"" + sceneToLoad.name + "\" (id: " + sceneToLoad.buildIndex + ")!");
				SceneManager.LoadScene(sceneToLoad.buildIndex);
			}
		}

I was trying to do the same thing earlier, and judging by your code (playerFinishedLevel && !playerDead), for the same reasons, and it didn’t work either…

It turns out that the Scene Manager has its own list of scenes, and the scenes on the Build Settings aren’t automatically added to it. From the documentation, there isn’t a function that adds a scene to the Scene Manager’s list of scenes without loading it. You can get the amount of scenes in the Build Settings with SceneManager.sceneCountInBuildSettings, but that’s it. In other words, what you’re trying to do here simply won’t work.

I’m still searching for a good solution for this problem, but for now I’m using this class I made:

public static class ScenesInBuild {
    public static readonly string[] scenes = {
        "Path_To_Scene_1",
        "Path_To_Scene_2"
    };
}

It seems that someone posted a better solution for this here (I haven’t tested it yet): How to get names of all available levels - Questions & Answers - Unity Discussions

Edit:

I looked at your code again, and noticed that, apart from that Debug line, you’re not actually using the sceneToLoad’s name at all, only its build index. If you don’t need its name, you could rewrite your code like this:

if (playerFinishedLevel && !playerDead) { //win
    disablePlayerPhysicsAndControls();
    if (Input.GetKey(KeyCode.Space)) { //trigger load
        int indexOfSceneToLoad = SceneManager.GetActiveScene().buildIndex + 1;
        Debug.Log("Loading level at id: " + indexOfSceneToLoad + "!");
        SceneManager.LoadScene(indexOfSceneToLoad);
    }
}

In my case, I was using the name of the scene I wanted to load, but I just realized that it wasn’t really necessary, so this is the method I’m using now. Also, I tested the SceneManager.sceneCountInBuildSettings and found that it doesn’t include disabled scenes, so you can use this to prevent from loading a scene that doesn’t exist, if you need to.

It’s a bug.
Solved by removing all scenes from Build Settings > Scenes in build and adding them again in the order you want. The index is assigned as you add the scenes to the list!

So, when does @UnityTechnologies plan to fix this? Because it STILL doesn’t work. I would love a fix, as this is a pain in the a** to work around.

I came up with a simple solution. If you name all your scenes lvl1, lvl2, lvl3… etc, you can load the next level by using it’s name and increasing the number. No need to make an array with a list of scenes. They all need to be added in build settings for this to work.

void LoadNextLevel()
{
	int nextLevelNumber = SceneManager.GetActiveScene().buildIndex + 2;	// add 2 because we count from lvl1 not lvl0 and we also want the next level not the current one
	string nextLevelName = "lvl" + nextLevelNumber;

	if (nextLevelNumber - 1 > SceneManager.sceneCountInBuildSettings - 1)	// if next level index is higher than the highest index
	{
		// level does not exist - show a warning or end the game

	} else
	{
		SceneManager.LoadScene(nextLevelName);	// load next level
	}
}

Apparently, if you add the LoadSceneMode, it will work. Some stupid bug in Unity:

SceneManager.LoadScene(“SceneName”, LoadSceneMode.Single);

In the menu, select “File” then “Build Settings”. This will open a small window. At the top of the window is a panel labelled “Scenes in Build”. Just under that panel is a button labelled “Add Open Scenes”. If you click that button it will add the currently open scene to your build. You can also drag and drop your scene files from the Project pane onto the “Scenes in Build” pane in this window to add those scenes to the build.

Your scenes must be included in the build in order to use SceneManager.LoadScene. If you haven’t added the scene to the Build Settings screen, you will get the out of range exception as you did above.