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 have the exact same issue and I've tried everything :( Come on Unity, get your bugs addressed first before adding loads new features to each new Unity version. I'm on 2018.1 and the bug is still here.
Answer by Naves · Mar 03, 2016 at 02:16 AM
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): http://answers.unity3d.com/questions/33263/how-to-get-names-of-all-available-levels.html
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.
Answer by HatrabbitSvarling · Jun 21, 2018 at 02:33 PM
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!
This didn't fix it for me. I'm using Unity 2018.1.6f1
Yeah I came to the same conclusion, got this error when opening Unity the next day, had no problems before. Removed and added them and all was fine.
Answer by Magius96 · Mar 02, 2016 at 10:53 PM
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.
There is a scene at buildIndex 1 in the Build Settings.
The error still occurs even with a scene in the buildIndex++ spot.
Answer by Kurdle_4855 · May 25, 2016 at 12:28 AM
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.
Please read my answer. It might be a little more convenient to use.
Answer by Pio6 · Jun 05, 2016 at 03:46 PM
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
}
}
Thanks for this! But the documentation says we should be able to reference scenes by an int called it's buildIndex, which is often preferable than relying on string-names for scenes.
Your answer
Follow this Question
Related Questions
C# SceneManager script not loading next scene after (seconds) 4 Answers
How to get scene name at certain buildIndex 5 Answers
[Unity Beginner] Game design question on scene loading and scene transitioning 0 Answers
How to unload an scene loaded in addictive mode ? 0 Answers
Load currently loaded scene but add one to the name number 2 Answers