• 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
Question by Tanoshimi2000 · Dec 21, 2015 at 07:55 PM · scenescene-loadingscene-switching

SceneManager.GetAllScenes() only returns the current scene

I've got a project where I simply want to put a scene menu. Here's the code:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.SceneManagement;
 
 public class PickScene : MonoBehaviour {
     Scene[] Scenes;
     
     // Use t$$anonymous$$s for initialization
     void Start () {
         Scenes = SceneManager.GetAllScenes();
     }
     
     // Update is called once per frame
     void Update () {
 
     }
 
     void OnGUI()
     {
         Debug.Log(SceneManager.GetAllScenes().Length);
         for (int i = 0; i < Scenes.Length; i++)
         {
             if (GUI.Button(new Rect(10, i*50,300,45), Scenes[i].name))
             {
                 SceneManager.LoadScene(i);
             }
         }
     }
 }

Works as expected, with no errors, but it only puts up a button for the Current scene, the Menu. There are two other scenes in the Build Settings, but they don't show up, so I can't test if t$$anonymous$$s is working. The Debug shows 1 Scene. Any idea why t$$anonymous$$s wouldn't show me how many scenes there are?

  • The documentation on t$$anonymous$$s is not complete, so I have nowhere else to turn.

Comment
FlyingHighUp
flashframe
domiii
JXPorter

People who like this

4 Show 12
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 Tanoshimi2000 · Dec 21, 2015 at 08:02 PM 1
Share

I just found sceneCountInBuildSettings(), and that works, giving me the correct number of scenes, but I still do not see a way to iterate through the Build Settings and get the list of scenes. Am I missing something?

avatar image FlyingHighUp Tanoshimi2000 · Dec 23, 2015 at 04:11 AM 0
Share

I'm having the same issue. I was under the impression GetAllScenes() would return all the scenes in build settings.

avatar image Tanoshimi2000 FlyingHighUp · Dec 23, 2015 at 12:14 PM 0
Share

I think it's a bug, so I reported it as a bug. Just not sure what the difference would be between sceneCount and sceneCountInBuildSettings. I'd think they'd be the same.

Show more comments
avatar image Kurdle_4855 Tanoshimi2000 · Jan 07, 2016 at 10:23 PM 0
Share

Ya, getSceneCountInBuildSettings returns the right number, but when I load a scene based off of the scene count it says the scene doesn't exist

avatar image JXPorter Tanoshimi2000 · Feb 16, 2017 at 11:34 PM 0
Share

Thank you for posting this answer. It solved my issue of how to replace the deprecated Application.levelCount with more contemporary code.

avatar image Tanoshimi2000 · Dec 23, 2015 at 03:55 PM 0
Share

@Leuthil Oh it 100% does give the count and list of loaded scenes only. In my scene menu app, clicking on the only button it made, the menu, reloaded the scene and increased the count of sceneCount by one each time.

But who wants the list of loaded scenes? I'd think it makes more sense to give us the list of scenes in the build settings, and then let us iterate that for whether isLoaded, or have a subset of that which a function like GetLoadedScenes() would return. GetAllScenes() should do exactly that.

I tried to loop through and load all the scenes to see if they'd show up in the list, but it actually loads them and makes them active. Yeah, I could do an asynchronous load, but then I'm loading every scene just to get the list of scenes so I can load the scene I want?

Nah, seems like we need GetLoadedScenes() and GetAllScenes(), or GetScenesInBuildSettings() (and an explanation of what the point of GetAllScenes() is for.)

My program is a demo of Cardboard that contains several different scenes, so I'd really like to be able to just DYNAMICALLY get a list of scenes, make buttons, and load the scene they choose. I do it now by manually creating the buttons for each scene in OnGUI(), but then I have to edit this script each time I add a new scene.

avatar image Leuthil Tanoshimi2000 · Dec 23, 2015 at 04:00 PM 2
Share

I agree. If there is a sceneCount and a sceneCountInBuildSettings, there should be a GetAllScenes() and a GetAllScenesInBuildSettings() or something like that.

avatar image FlyingHighUp Tanoshimi2000 · Dec 23, 2015 at 04:36 PM 0
Share

Yeah this totally irks me. My game is a online game with multiple levels for online play - I want to allow the levels to cycle, and for players to vote on the level they want next. It would be nice if my list of build settings game levels were automatically added to a list.

Until they give us a GetScenesInBuildSettings(), you can get a list of build settings scenes using EditorBuildSettings.scenes http://docs.unity3d.com/412/Documentation/ScriptReference/EditorBuildSettings-scenes.html

The catch is it's UnityEditor only. But you can create a custom inspector that saves it to a Monobehaviour list.

They are ordered, so remove the non-enabled scenes and you can get the buildIndex. and to get the name, use Path.GetFileNameWithoutExtension(scenePathFromBuildSettings);

avatar image Tanoshimi2000 FlyingHighUp · Dec 23, 2015 at 04:59 PM 0
Share

@FlyingHighUp Now that's an interesting solution! I think I can use pre-compiler directives so that in the Editor, it keeps the list up to date, and then in standalone mode, it just reads the list of scenes.

I'd call that a solution. Please change your comment to a reply so I can mark this as a solution and give you props.

Show more comments

3 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by FlyingHighUp · Dec 23, 2015 at 05:42 PM

Until they give us a GetScenesInBuildSettings(), you can get a list of build settings scenes using EditorBuildSettings.scenes http://docs.unity3d.com/412/Documentation/ScriptReference/EditorBuildSettings-scenes.html

The catch is it's UnityEditor only. But you can create a custom inspector that saves it to a list in some Monobehaviour.

They are ordered, so remove the non-enabled scenes and you can get the buildIndex. To get the scene name, use Path.GetFileNameWithoutExtension(scenePathFromBuildSettings);

Comment
flashframe
Galaks
domiii
glenneroo

People who like this

4 Show 2 · 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 Tanoshimi2000 · Dec 23, 2015 at 07:24 PM 1
Share

Well, this worked. I've got a nice menu interface that gathers the list of scenes in editor mode, and then makes buttons that allow you to select the level. Works on the cell phone. NOW I have a cardboard demo! Thanks!

avatar image flashframe · Jan 28, 2016 at 07:16 PM 0
Share

This is great, really helped me out. Thanks!

avatar image

Answer by manelroure · Mar 21, 2016 at 11:27 PM

I had the same problem and with the help of t$$anonymous$$s post I could fix it. I t$$anonymous$$nk t$$anonymous$$s code could help those who want to solve the same problem.

Thanks! :)

 using UnityEditor;
 using System.IO;
 
     EditorBuildSettingsScene[] allScenes = EditorBuildSettings.scenes;
     Debug.Log ("All Scenes : Length : "+allScenes.Length);
     string path;
     for (int i = 0; i < allScenes.Length; i++) {
         Debug.Log ("All Path : Scene : "+allScenes[i].path);
         path = Path.GetFileNameWithoutExtension (allScenes[i].path);
         Debug.Log ("Clear Path : Scene : "+path);
     }
Comment
FlyingHighUp

People who like this

1 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 Tom980 · Jan 12, 2021 at 04:09 PM 0
Share

Fair warning for anyone who's looking to export their game in the future: This code is not for you!

According to the current best answer from FlyingHighUp EditorBuildSettings.scenes only works in the Editor, therefore preventing you from building any projects you use this type of code in.

avatar image

Answer by Quatum1000 · Jul 22, 2019 at 12:29 AM

The confusing t$$anonymous$$ng is, there is a EditorSceneManager and a SceneManager. The SceneManager works also in the Editor as well.

T$$anonymous$$s example fills a scenes array with all scenes in the $$anonymous$$erarchy.

 using UnityEngine.SceneManagement;    
 Scene[] Scenes = new Scene[SceneManager.sceneCount];
     for (int i = 0; i < SceneManager.sceneCount; i++) {
         Scenes[i] = SceneManager.GetSceneAt(i);
         Debug.Log(Scenes[i].name);
     }
 

Comment

People who like this

0 Show 0 · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

38 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

Related Questions

How can I make a loading screen with LoadSceneAsync 3 Answers

Trying to access SceneManagement 1 Answer

How to ensure that an additively loaded scene is active before it becomes interactive? 1 Answer

Using parameters of activeSceneChanged? 0 Answers

It's impossible to async load a scene then have it wait while unloading another? 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