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): PlayerPrefs.SetString(GetNextSceneName(), null); string GetNextSceneName() { // my current scene int activeSceneBuildIndex = SceneManager.GetActiveScene().buildIndex; // check if there is another scene in build settings after this one if (activeSceneBuildIndex + 1 < SceneManager.sceneCountInBuildSettings) return SceneManager.GetSceneByBuildIndex(activeSceneBuildIndex + 1).name; // !!! else return string.Empty; } 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 ++
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): public class SceneWrap { public Scene m_Scene; public eSceneLoadState m_State; // forget this. it's a custom enum. public AsyncOperation m_Operation; } Code (CSharp): public IList<SceneWrap> GetAllScenes() { if (m_Scenes == null) { m_Scenes = new List<SceneWrap>(); for (int i = 0; i < SceneManager.sceneCountInBuildSettings; i++) { m_Scenes.Add(new SceneWrap() { m_Scene = SceneManager.GetSceneByBuildIndex(i), m_State = SceneManager.GetSceneByBuildIndex(i).isLoaded ? eSceneLoadState.Loaded : eSceneLoadState.UnLoaded, m_Operation = null }); } } return m_Scenes; } 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. 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 !
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): string path = SceneUtility.GetScenePathByBuildIndex(i); string sceneName = path.Substring(0, path.Length - 6).Substring(path.LastIndexOf('/') + 1); see if it fit your case or not.
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);
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
? You have the scene loaded and the GetSceneByBuildIndex returns null? You should submit a bug report.
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.
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 ++