Search Unity

GetSceneByBuildIndex() problem

Discussion in 'Scripting' started by Geckoo, Jan 23, 2017.

  1. Geckoo


    Dec 7, 2014
    Hi friends.
    In my project I have to read the name of the next scene in order to enable/disable menu buttons. So I use this code just to write in PlayerPrefs a string - the name of the next scene.
    Code (csharp):
    2. PlayerPrefs.SetString(GetNextSceneName(), null);
    4. string GetNextSceneName()
    5. {   // my current scene
    6.     int activeSceneBuildIndex = SceneManager.GetActiveScene().buildIndex;
    7.     // check if there is another scene in build settings after this one
    8.     if (activeSceneBuildIndex + 1 < SceneManager.sceneCountInBuildSettings)
    9.         return SceneManager.GetSceneByBuildIndex(activeSceneBuildIndex + 1).name; // !!!
    10.     else
    11.         return string.Empty;
    12. }
    However I have always the same result - null. And I have more than 10 scenes in this project.
    I don't understand why this code doesn't work. Did I a mistake?

    Trying to solve this problem, I used some Debug.Log lines and this one works like a charm :
    Debug.Log(SceneManager.GetSceneByBuildIndex(activeSceneBuildIndex).name); // no increment
    Why I cannot increment my index? It's weird...

    If you have an idea, please explain me. Thank you ++
    JokerZappie likes this.
  2. canis


    Oct 25, 2013
    I also notice the similar problems, for GetSceneByBuildIndex(int);
    seem like the variable it return was wrong, if that scene wasn't loaded before.
    here is the code that I wanted to get all the information from SceneManager.

    Code (CSharp):
    1. public class SceneWrap
    2. {
    3.     public Scene m_Scene;
    4.     public eSceneLoadState m_State; // forget this. it's a custom enum.
    5.     public AsyncOperation m_Operation;
    6. }
    Code (CSharp):
    1. public IList<SceneWrap> GetAllScenes()
    2. {
    3.     if (m_Scenes == null)
    4.     {
    5.         m_Scenes = new List<SceneWrap>();
    6.         for (int i = 0; i < SceneManager.sceneCountInBuildSettings; i++)
    7.         {
    8.             m_Scenes.Add(new SceneWrap()
    9.             {
    10.                 m_Scene = SceneManager.GetSceneByBuildIndex(i),
    11.                 m_State = SceneManager.GetSceneByBuildIndex(i).isLoaded ? eSceneLoadState.Loaded : eSceneLoadState.UnLoaded,
    12.                 m_Operation = null
    13.             });
    14.         }
    15.     }
    16.     return m_Scenes;
    17. }
    in my test project, I already added 3 scene in build setting.
    and the result was partially correct..

    3 scenes = { Index, MainMenu, Lobby }

    Editor switch to "Index" scene, click "play"
    the above code was run on Awake() with no error, and the break point on return, it tell me the following reference.

    m_Scenes.Count = 3; // correct
    m_Scenes[0] // Correct, it's the scene playing on editor.

    m_Scenes[1] & m_Scenes[2] // Fail, they look the same in reference. scene02.JPG

    hope there are someone can give me answer, otherwise I'll need to cache the scene list on my own....

    Well... I answer myself...

    it said :
    What's the point for naming a method "By Build Index" but cannot return the valid result if it's not yet being used?!
    at least.. give me the correct name ! dude !

    Last edited: Feb 6, 2017
    ledbetterMRI and Aka_ToolBuddy like this.
  3. canis


    Oct 25, 2013
    again, here is the solution for my problems, hope it can help.
    for me, I need the scene name for serialize data and reading

    by using UnityEngine.SceneManagement.SceneUtility, there is another method,

    this one can return correct path, even it's not loaded yet.
    the code for crop the scene name from path as following.

    Code (CSharp):
    1. string path = SceneUtility.GetScenePathByBuildIndex(i);
    2. string sceneName = path.Substring(0, path.Length - 6).Substring(path.LastIndexOf('/') + 1);
    see if it fit your case or not.
    lovk4ch, Morseliot, Sibason and 7 others like this.
  4. levwsr


    Jul 23, 2012
    stupid unity bug in Unity 2017.. here was our workaround:
    string pathToScene = SceneUtility.GetScenePathByBuildIndex(0);
    string sceneName = System.IO.Path.GetFileNameWithoutExtension(pathToScene);
    Debug.Log("LgCoreReloader: Reloading to scene(0): " + sceneName);
    QuantumCalzone and nemesis92 like this.