Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
5
Question by auzzyWiz · Nov 14, 2017 at 09:16 PM · scenesscene load

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
8
Best Answer

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.

Comment
Add comment · Show 8 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Tourist · Nov 15, 2017 at 07:26 PM 0
Share

Well, I didn't know that either.

avatar image auzzyWiz · Nov 16, 2017 at 04:40 AM 0
Share

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.

avatar image MarshallN auzzyWiz · Nov 16, 2017 at 05:24 PM 0
Share

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?

avatar image auzzyWiz · Nov 16, 2017 at 08:49 PM 0
Share

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!

avatar image auzzyWiz · Nov 16, 2017 at 08:58 PM 0
Share

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

avatar image MarshallN auzzyWiz · Nov 16, 2017 at 09:03 PM 0
Share

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.

avatar image MarshallN auzzyWiz · Nov 16, 2017 at 09:10 PM 0
Share

You could also use the amazing @Bunny83 's script here to get a list of all of your scene names, and adapt it (since it should look at them in build order) to build a Dictionary that matches string name to int buildIndex.

All things considered, I think that'd be my recommendation.

avatar image auzzyWiz · Nov 17, 2017 at 12:09 AM 0
Share

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!

avatar image
7

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);
 }
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image vessago · Oct 05, 2018 at 05:21 AM 2
Share

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;
         }

avatar image KyleChallis vessago · Jan 23 at 08:26 PM 0
Share

Thank you! I hate that you have to loop through everything to get it, but this works.

avatar image Amitloaf · Nov 06, 2018 at 09:42 AM 0
Share

This works. You can also use Path.GetFileNameWithoutExtension(path) ins$$anonymous$$d of the manual parsing.

avatar image
0

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image auzzyWiz · Nov 16, 2017 at 04:41 AM 0
Share

I saw that too... but no it is not from an assetbundle per my understanding.

avatar image
0

Answer by rachidDev · Aug 22, 2018 at 11:41 PM

SceneManager.GetActiveScene().buildIndex

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image felix_of_mars · Dec 10, 2018 at 02:57 PM 0
Share

As the name suggests, only gets the active scene. Which is the problem with using GetSceneName also.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

81 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges