Search Unity

Something is definately wrong with the SceneManager

Discussion in 'Editor & General Support' started by deLord, Feb 5, 2019.

  1. deLord

    deLord

    Joined:
    Oct 11, 2014
    Posts:
    306
    My Android build was running well inside the Editor and with Unity Remote. Then I wanted to build and it gives me the error
    IndexOutOfRangeException: Scene index "1" is out of range.
    I had this error before and I am unsure why it resolved. I tried clearing all scenes from the build settings and adding them again which didnt work.

    My code looks like this (had to use internal because the method is static)
    Code (CSharp):
    1. internal static List<string> getLevelNames() {
    2.         List<string> levelNames = new List<string>();
    3.         for (int i = 0; i < SceneManager.sceneCountInBuildSettings - 2; i++) {
    4.          
    5.             string path = SceneManager.GetSceneAt(i).path;
    6.             if (SceneManager.GetSceneAt(i).IsValid() && path.Contains("/lv")) {
    7.                 //EditorBuildSettingsScene[] scenes = EditorBuildSettings.scenes;    // the old code which worked for Unity Remote
    8.                 //for (int i = 0; i < scenes.Length; ++i) {
    9.                 // if (scenes[i].enabled && scenes[i].path.Contains("/lv")) {
    10.                 string lvName = path.Substring(path.IndexOf("/lv") + 1);
    11.                 lvName = lvName.Substring(0, lvName.IndexOf(".unity"));
    12.                 levelNames.Add(lvName);
    13.             }
    14.         }
    15.         return levelNames;
    16.     }
    I have 7 scenes, all active, as you can see in the screenshot.
    SceneManager.png
     
  2. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,637
    darshan-kirubakaran likes this.
  3. deLord

    deLord

    Joined:
    Oct 11, 2014
    Posts:
    306
    thank you, works like a charm. Wonder why I never saw that :confused:
     
  4. chino_noris

    chino_noris

    Joined:
    Feb 1, 2019
    Posts:
    5
    The confusion is that you should be using
    Code (CSharp):
    1. SceneManager.GetSceneByBuildIndex(i)
    instead of
    Code (CSharp):
    1. SceneManager.GetSceneAt(i)
    .
     
    Verdant88 and AnaLuyza like this.