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
6
Question by MattDahEpic · Mar 02, 2016 at 09:00 PM · c#scene-loadingscene load

SceneManager not finding scene at buildIndex, even though there is one.

I have a "level advancement" system, and I am trying to load the level with the buildIndex of the current level plus 1 (the next level). The SceneManager throws IndexOutOfRangeException: Scene index "1" is out of range.at the SceneManager.GetSceneAt line whenever I attempt to load the scene. There is a scene at buildIndex 1 in the Build Settings.

Code:

 if (playerFinishedLevel && !playerDead) { //win
             disablePlayerPhysicsAndControls();
             if (Input.GetKey(KeyCode.Space)) { //trigger load
                 Scene sceneToLoad = SceneManager.GetSceneAt(SceneManager.GetActiveScene().buildIndex + 1);
                 Debug.Log("Loading level \"" + sceneToLoad.name + "\" (id: " + sceneToLoad.buildIndex + ")!");
                 SceneManager.LoadScene(sceneToLoad.buildIndex);
             }
         }

Comment
Add comment · Show 1
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 GregoryFenn · Jul 13, 2018 at 08:34 PM 0
Share

I have the exact same issue and I've tried everything :( Come on Unity, get your bugs addressed first before adding loads new features to each new Unity version. I'm on 2018.1 and the bug is still here.

6 Replies

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

Answer by Naves · Mar 03, 2016 at 02:16 AM

I was trying to do the same thing earlier, and judging by your code (playerFinishedLevel && !playerDead), for the same reasons, and it didn't work either...

It turns out that the Scene Manager has its own list of scenes, and the scenes on the Build Settings aren't automatically added to it. From the documentation, there isn't a function that adds a scene to the Scene Manager's list of scenes without loading it. You can get the amount of scenes in the Build Settings with SceneManager.sceneCountInBuildSettings, but that's it. In other words, what you're trying to do here simply won't work.

I'm still searching for a good solution for this problem, but for now I'm using this class I made:

 public static class ScenesInBuild {
     public static readonly string[] scenes = {
         "Path_To_Scene_1",
         "Path_To_Scene_2"
     };
 }
 

It seems that someone posted a better solution for this here (I haven't tested it yet): http://answers.unity3d.com/questions/33263/how-to-get-names-of-all-available-levels.html

Edit:

I looked at your code again, and noticed that, apart from that Debug line, you're not actually using the sceneToLoad's name at all, only its build index. If you don't need its name, you could rewrite your code like this:

 if (playerFinishedLevel && !playerDead) { //win
     disablePlayerPhysicsAndControls();
     if (Input.GetKey(KeyCode.Space)) { //trigger load
         int indexOfSceneToLoad = SceneManager.GetActiveScene().buildIndex + 1;
         Debug.Log("Loading level at id: " + indexOfSceneToLoad + "!");
         SceneManager.LoadScene(indexOfSceneToLoad);
     }
 }

In my case, I was using the name of the scene I wanted to load, but I just realized that it wasn't really necessary, so this is the method I'm using now. Also, I tested the SceneManager.sceneCountInBuildSettings and found that it doesn't include disabled scenes, so you can use this to prevent from loading a scene that doesn't exist, if you need to.

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
2

Answer by HatrabbitSvarling · Jun 21, 2018 at 02:33 PM

It's a bug. Solved by removing all scenes from Build Settings > Scenes in build and adding them again in the order you want. The index is assigned as you add the scenes to the list!

Comment
Add comment · 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 GregoryFenn · Jul 13, 2018 at 08:32 PM 1
Share

This didn't fix it for me. I'm using Unity 2018.1.6f1

avatar image Leniaal · Sep 07, 2018 at 09:41 AM 0
Share

Yeah I came to the same conclusion, got this error when opening Unity the next day, had no problems before. Removed and added them and all was fine.

avatar image
-1

Answer by Magius96 · Mar 02, 2016 at 10:53 PM

In the menu, select "File" then "Build Settings". This will open a small window. At the top of the window is a panel labelled "Scenes in Build". Just under that panel is a button labelled "Add Open Scenes". If you click that button it will add the currently open scene to your build. You can also drag and drop your scene files from the Project pane onto the "Scenes in Build" pane in this window to add those scenes to the build.

Your scenes must be included in the build in order to use SceneManager.LoadScene. If you haven't added the scene to the Build Settings screen, you will get the out of range exception as you did above.

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 MattDahEpic · Mar 02, 2016 at 11:02 PM 0
Share

There is a scene at buildIndex 1 in the Build Settings.

The error still occurs even with a scene in the buildIndex++ spot.

avatar image
0

Answer by Kurdle_4855 · May 25, 2016 at 12:28 AM

So, when does @UnityTechnologies plan to fix this? Because it STILL doesn't work. I would love a fix, as this is a pain in the a** to work around.

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 Pio6 · Jun 05, 2016 at 03:49 PM 0
Share

Please read my answer. It might be a little more convenient to use.

avatar image
0

Answer by Pio6 · Jun 05, 2016 at 03:46 PM

I came up with a simple solution. If you name all your scenes lvl1, lvl2, lvl3... etc, you can load the next level by using it's name and increasing the number. No need to make an array with a list of scenes. They all need to be added in build settings for this to work.

 void LoadNextLevel()
 {
     int nextLevelNumber = SceneManager.GetActiveScene().buildIndex + 2;    // add 2 because we count from lvl1 not lvl0 and we also want the next level not the current one
     string nextLevelName = "lvl" + nextLevelNumber;
 
     if (nextLevelNumber - 1 > SceneManager.sceneCountInBuildSettings - 1)    // if next level index is higher than the highest index
     {
         // level does not exist - show a warning or end the game
 
     } else
     {
         SceneManager.LoadScene(nextLevelName);    // load next level
     }
 }
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 GregoryFenn · Jul 13, 2018 at 08:36 PM 0
Share

Thanks for this! But the documentation says we should be able to reference scenes by an int called it's buildIndex, which is often preferable than relying on string-names for scenes.

  • 1
  • 2
  • ›

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

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

C# SceneManager script not loading next scene after (seconds) 4 Answers

How to get scene name at certain buildIndex 5 Answers

[Unity Beginner] Game design question on scene loading and scene transitioning 0 Answers

How to unload an scene loaded in addictive mode ? 0 Answers

Load currently loaded scene but add one to the name number 2 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