- Home /
How to get BuildIndex from scene name?
I am trying to get the BuildIndex of a scene by name.
Scene nextScene = SceneManager.GetSceneByName("Scene1");
GameControllerMain.nextLevel = nextScene.buildIndex;
Debug.Log(sceneName + " Index:"+GameControllerMain.nextLevel);
The debug however spits out -1 every time...
"Scene1" is the name of the scene file within "Assets/Scenes/".
The scene has already been added to build settings as well.
Answer by MarshallN · Nov 15, 2017 at 05:30 PM
SceneManager.GetSceneByName can only get scenes added to the SceneManager, which is to say only the loaded scenes.
I'm gonna say that again, because the documentation doesn't do a good job of explaining it. Only scenes currently loaded and active are added to the SceneManager. SceneManager manages active scenes.
As it currently stands, there's no good way to do what you want to do. SceneUtility.GetBuildIndexByScenePath can do it if you have the path, not just the name, but it's definitely not ideal. Might work for you, though.
Yes, this is the conclusion I have come to as well. Thank you for more clarity. Is there a way to load a scene without displaying it? $$anonymous$$aybe that is what I am missing too.
You could put all objects in the scene as children of an inactive empty GameObject so that nothing happens when it loads until you enable that object, though you'd have to watch out for loading too many objects (even inactive ones) into memory and slowing down performance, so it would only be useful for one scene at a time unless you have very simple scenes.
You should be able to use LoadSceneAsync() to load the scene in the background, and then when the scene is loaded (at the end of the IEnumerator in which you're loading the scene) get the build index from the scene manager just like you were originally, then immediately UnloadSceneAsync() to get rid of it. Again, this would work best if everything in your scene were disabled until manually activated by a script, so that you don't have to deal with things appearing in the middle of the scene you actually want active before disappearing just as suddenly.
What's your goal here? What are you trying to do in your game?
Yeah, there has to be a better way to accomplish what I am trying to do.
I have several scenes in my project such as "_Intro", "_StartScreen","_LevelSelect", etc.
I then have my actual level scenes named "Scene1","Scene2","Scene3", and so forth. Unfortunately, it is harder to iterate through these scenes sometimes as I have to parse the string to get the level number. Ugh... not ideal... and I am running into issues doing it this way.
I have intermission scenes that need to get data about the next scene and it's just not manageable the way I am doing it. I'm just hacking it together.
The buildindex does not correspond with the level number so it's a pain and I am not sure how all this is handled.
Your time and advice is very appreciated!
I could just name the scenes "1","2","3", etc... Is that how others are doing this? I could also put the intro at buildindex 0 and then scene1 at buildindex 1 followed by other level scenes and concluded by my "_StartScreen","_LevelSelect", etc..
That's a solution, for sure - relatively easy to manage. The other thing I might recommend is to bundle all your level scenes into an AssetBundle and load them from there. That way, you can be sure that your index within the bundle is only looking at the actual level scenes and not the 'support' scenes you don't want to parse.
Nice! Great info. I will look into the asset bundle as that was mentioned by someone else too. Will check out @Bunny83 script as well. $$anonymous$$eep you posted on the route taken. Thank you!
Answer by JimmyCushnie · Apr 18, 2018 at 06:50 PM
I managed to figure this out. Here you go, future googlers:
private static string NameFromIndex(int BuildIndex)
{
string path = SceneUtility.GetScenePathByBuildIndex(BuildIndex);
int slash = path.LastIndexOf('/');
string name = path.Substring(slash + 1);
int dot = name.LastIndexOf('.');
return name.Substring(0, dot);
}
And to get Scene name from that:
private int sceneIndexFromName(string sceneName) {
for (int i = 0; i < Scene$$anonymous$$anager.sceneCountInBuildSettings; i++) {
string testedScreen = NameFromIndex(i);
//print("sceneIndexFromName: i: " + i + " sceneName = " + testedScreen);
if (testedScreen == sceneName)
return i;
}
return -1;
}
Thank you! I hate that you have to loop through everything to get it, but this works.
This works. You can also use Path.GetFileNameWithoutExtension(path) ins$$anonymous$$d of the manual parsing.
Answer by Tourist · Nov 15, 2017 at 03:51 PM
Was your scene loaded from an AssetBundle? If so, it is the proper behaviour according to their documentation : https://docs.unity3d.com/ScriptReference/SceneManagement.Scene-buildIndex.html
I saw that too... but no it is not from an assetbundle per my understanding.
Answer by rachidDev · Aug 22, 2018 at 11:41 PM
SceneManager.GetActiveScene().buildIndex
As the name suggests, only gets the active scene. Which is the problem with using GetSceneName also.
Your answer
Follow this Question
Related Questions
Waiting Time Difference Between 720p and 1080p 0 Answers
Loading Specific Scenes Post-Build 0 Answers
Scene.isLoaded not working properly. 1 Answer
Load Scene from .unity File 1 Answer
Download scene from server 1 Answer