I have 3 scenes in my Unity project and trying to get a scene index by its name. I’m trying to use SceneManager but I can’t figure out how to use SceneManager.GetSceneByName . Using
SceneManager.GetSceneByName("Scene1");
returns null
while I can load the scene using
SceneManager.LoadScene("Scene1",LoadSceneMode.Single);
Also SceneManager.sceneCount returns 1
while I have 3 scenes.
Unity documents for GetSceneByName
says:
Searches through the scenes added to the SceneManager for a scene with the given name.
The name has to be without the .unity extension. The name can be the
last part of the name as displayed in the BuildSettings window in
which case the first scene that matches will be returned. The name
could also the be path as displayed in the Build Settings, still
without the extension, in which case only the exact match will be
returned. This is case insensitive.
Does it mean I should add all the scenes to the SceneManager
first?
How can I do that?
If so, then why SceneManager.LoadScene()
works with the scene name without adding it to the SceneManager
?
Note: I’ve already added all 3 scenes to the Build setting.