Search Unity

GetSceneByBuildIndex() problem

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

  1. Geckoo

    Geckoo

    Joined:
    Dec 7, 2014
    Posts:
    144
    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):
    1.  
    2. PlayerPrefs.SetString(GetNextSceneName(), null);
    3.  
    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. }
    13.  
    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

    canis

    Joined:
    Oct 25, 2013
    Posts:
    79
    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.
    scene01.JPG

    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...

    Ref:
    https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager.GetSceneByBuildIndex.html
    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
    zhuchun, Procyonx_, dman8723 and 2 others like this.
  3. canis

    canis

    Joined:
    Oct 25, 2013
    Posts:
    79
    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,
    GetScenePathByBuildIndex(int);

    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.
     
  4. levwsr

    levwsr

    Joined:
    Jul 23, 2012
    Posts:
    68
    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);
     
  5. French_Baguette

    French_Baguette

    Joined:
    Nov 22, 2019
    Posts:
    1

    Just wanna mention this helped! Was wondering why they would provide that functionality but it was returning null for a scene that was definitely there??? Poor documentation and wording on their part but I guess its one of the cons of Unity
     
    halley likes this.
  6. kobyfr

    kobyfr

    Joined:
    Aug 21, 2020
    Posts:
    7
    nicely done
     
  7. a_loupas

    a_loupas

    Joined:
    Mar 17, 2021
    Posts:
    16
    Getting null with unity 2021.2 too. This needs to be fixed.
     
  8. DevinW

    DevinW

    Joined:
    Jun 19, 2014
    Posts:
    37
    Same - this is still returning null. Going to have to do a manual asset data base search.
     
  9. ? You have the scene loaded and the
    GetSceneByBuildIndex
    returns null? You should submit a bug report.
     
  10. qiwulun

    qiwulun

    Joined:
    Nov 26, 2019
    Posts:
    1
    This API has been unreliable since 2017. GetSceneByBuildIndex(activeSceneBuildIndex). Name, for this usage, the name obtained is not consistent in case. The name of my scene is "hell", which displays "Hell" in the editor, but after building, it displays "hell" again.
     
    Last edited: Aug 28, 2022
  11. Geckoo

    Geckoo

    Joined:
    Dec 7, 2014
    Posts:
    144
    What are you doing? I wrote this post a few years ago. If you have a request, post a new one on the forum. You dig up a post from the past - almost a dinosaur. Don't expect anything ++