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 /
  • Help Room /
avatar image
0
Question by Ingvaras · Oct 25, 2016 at 07:00 PM · c#scene-loadingscenes

How to get scene name at certain buildIndex

There is way to get currently loaded scene name by Application.loadedLevelName, but is there a way to get a name of the scene at certain buildIndex or a name of the scene that is at current buildIndex + 1?

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

5 Replies

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

Answer by hexagonius · Oct 25, 2016 at 08:20 PM

I think the only access point is mentioned here:

https://docs.unity3d.com/ScriptReference/EditorBuildSettings-scenes.html

The 5.5b documentation states, that a EditorBuildSettingsScene only has a path as the only string information. The exampled from the link above makes use of the AssetDatabase class to get the full path which contains the name of the level (acutally I don't know if not the path from EditorBuildSettingsScene already contains that).
Nevertheless, it seems a little complicated, but that's the best I came up with.

Comment
Add comment · 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
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 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 vlad-angelov · Sep 06, 2018 at 10:36 AM 1
Share

Thank you, my friend. You saved my day. <3

avatar image
5

Answer by ibx00 · Feb 04, 2020 at 09:47 AM

 System.IO.Path.GetFileNameWithoutExtension(SceneUtility.GetScenePathByBuildIndex(yourBuildIndex));

GetScenePathByBuildIndex returns the path and GetFileNameWithoutExtension returns the name of the file without the path and extension. So the result of this line is the exact scene name as per the given index you've given

Cheers :)

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 darsheel88 · Feb 04, 2020 at 10:10 AM 0
Share

Works for me.. thanks

avatar image
1

Answer by JPoenisch · Jul 04, 2019 at 04:48 AM

There is an easier way:

 private static string GetSceneNameByIndex(int buildIndex)
 {
     if(buildIndex > SceneManager.sceneCountInBuildSettings - 1)
     {
         Debug.LogErrorFormat("Incorrect buildIndex {0}!", buildIndex);
         return null;
     }
 
     var scene = SceneManager.GetSceneByBuildIndex(buildIndex);
     return  scene.name;
 }

 private static string GetNextSceneName()
 {
     return GetSceneNameByIndex(SceneManager.GetActiveScene().buildIndex + 1);
 }

Comment
Add comment · Show 4 · 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 akaBase · Mar 31, 2020 at 11:48 AM 0
Share

If the buildIndex is the actual buildIndex as shown in the editor build settings window, you don't need to +1 to Scene$$anonymous$$anager.sceneCountInBuildSettings.

Other than that this is the best answer imo.

avatar image JPoenisch akaBase · Mar 31, 2020 at 12:36 PM 0
Share

I think it should have been a - ^^ Don't know why I have put a + there actually

avatar image akaBase JPoenisch · Mar 31, 2020 at 01:55 PM 0
Share

Don’t need to do anything to it

Show more comments
avatar image
0

Answer by stslavik · Jul 29, 2019 at 01:57 AM

Came looking for the same issue, and got hung up on the SceneManager handling already loaded scenes, which didn't necessarily help. Here's the solution I came up with:

 public static string GetSceneNameFromBuildIndex(int index) {
    string scenePath = SceneUtility.GetScenePathByBuildIndex(index);
    string sceneName = System.IO.Path.GetFileNameWithoutExtension(scenePath);
 
    return sceneName;
 }
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 meethi · Jul 17, 2020 at 12:11 PM 0
Share

Thanks - worked perfectly

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

85 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 avatar image avatar image avatar image avatar image

Related Questions

Save timer score between scenes? (C#) 5 Answers

Changing player position when changing scene 1 Answer

Why does my script and collider2D automatically deactivate after reloading the scene? 0 Answers

Asset Bundle Scene Loading Bug???? 0 Answers

How to fix Loading Screen? 0 Answers


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