Context
I was working with asynchronous scene transitions for my game when I stumbled across an error that had me stuck for a day and a half. The error said that the scene I was transitioning to was invalid (had a build index of -1, which means the scene couldn’t be found).
After much experimenting, I realized that there was no problem loading a scene from a number or string.
For instance these worked fine:
SceneManager.LoadSceneAsync(0); // Build Index
SceneManager.LoadSceneAsync("Main Menu"); // Scene Name
However, these functions would return an empty Scene:
Scene scene1 = SceneManager.GetSceneByBuildIndex(0);
Scene scene2 = GetSceneByName("Main Menu");
Debug.Log(scene1.name); // Prints ""
Debug.Log(scene2.name); // Prints ""
Debug.Log(scene1.buildIndex); // Prints "-1"
Debug.Log(scene2.buildIndex); // Prints "-1"
So when I tried to load a scene with a build index I got from SceneManager.GetSceneByName(“Main Menu”).buildIndex;, the scene would be invalid.
My Conclusion
According to the forums, SceneManager’s ‘scene getters’ only get scenes that are loaded. But this feels like it defeats the purpose of a getter. Shouldn’t SceneManager.GetActiveScene() handle loaded scenes and the other getters search through the build scenes?
Questions
Is this a bug or a problem with my code or Unity settings?
If this is working as intended, what’s the point of it working this way? Why can Scene Manager ‘load’ from a string/buildID, but can’t ‘get’ from a string/buildID? And how can I get the build index of a build scene by its name?