4

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.

Ghasem
  • 14,455
  • 21
  • 138
  • 171

1 Answers1

6

The method SceneManager.GetSceneByName() searches the scene in the list of scenes which has been loaded. As far as SceneManager.sceneCount goes, the unity documentation for it clearly states that it returns the :

number of currently loaded scenes

For finding out the number of scenes in the build settings, there is a separate variable SceneManager.sceneCountInBuildSettings.

If you use SceneManager.GetSceneByName(), after loading the scene in additive mode, then a valid scene is returned.

greenPadawan
  • 1,511
  • 3
  • 17
  • 30
  • If by *scenes which has been loaded* you mean the scenes which has been played, then I've already tried that. after loading other scenes and get back to the first one, it still can't find them by name. unless it will remove other scenes as soon as the scene is changed. – Ghasem Aug 06 '16 at 08:35
  • 1
    No, I don't mean that. When you load a scene(in single mode), then all current loaded scenes are closed and the new scene is loaded. That's why I mentioned loading the scene in the additive mode. – greenPadawan Aug 06 '16 at 08:39
  • When I use Additive mode, it will merge the loaded scene object with the current scene, Am I doing it wrong? `SceneManager.LoadScene("Scene1", LoadSceneMode.Additive);` – Ghasem Aug 06 '16 at 08:58
  • 1
    @AlexJolig, you rarely, rarely, rarely use "additive mode" especially if you're a beginner. Almost certainly you do not want that. Just use ordinary scene loading. – Fattie Aug 06 '16 at 13:35
  • @AlexJolig, yes when using the Additive mode, it will merge the given scene with the current scene. As @Joe Blow mentioned, you most probably want to just load scene in single mode. However, in order to use `GetSceneByName` the scene which you are searching, should be loaded. – greenPadawan Aug 07 '16 at 07:43