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.