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
1
Question by HarisKap · Dec 12, 2015 at 02:28 PM · scene-loading5.3

5.3 SceneManager.GetSceneByName bug?

The following method works and loads the scene.

But sceneToLoad is null why? Is intended? wasn't supposed to give back the scene?

 public static void LoadScene(string sceneName)
 {
     Debug.Log(sceneName);

     Scene sceneToLoad = SceneManager.GetSceneByName(sceneName);

     Debug.Log(sceneToLoad.name);

     SceneManager.LoadScene(sceneName);
 }
Comment
Add comment · Show 2
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 MasterHoover · Apr 05, 2016 at 05:36 AM 0
Share

It doesn't work for me either.

This works :

 Scene$$anonymous$$anager.LoadScene (levelName);

This doesn't

 Scene$$anonymous$$anager.LoadScene (Scene$$anonymous$$anager.GetSceneByName (levelName).buildIndex);

GetSceneByName simply doesn't work.

avatar image saschandroid MasterHoover · Apr 05, 2016 at 05:49 AM 0
Share

It finds the scene, if you have added the scene to the hierarchy. I suppose that's what they mean by 'Searches through the scenes added to the Scene$$anonymous$$anager for a scene with the given name.'

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by $$anonymous$$ · Jan 20, 2016 at 08:22 PM

@HarisKap

at least this way seems legit to me, but it also returns -1, even all the scenes are in my build settings:

 public void LoadRequestedLevel(string levelName)
       {
         int nextLevel = SceneManager.GetSceneByName(levelName).buildIndex;
         print(nextLevel);
     }

Does anyone have a fix for this alreay?

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 unity_HxXYq2JUbx4_8g · Apr 08, 2020 at 10:47 PM 0
Share

Sorry to necro this but I always feel bad when someone doesn't get a simple answer to a simple question. Plus, I've just bumped into this issue and found why.

If you hover your mouse over the Scene$$anonymous$$anager.GetSceneByName method, you'll read : "Searches through the Scenes loaded for a Scene with the given name."

So it's doesn't have to do with the scenes in your build settings, but rather with the scenes that are ALREADY loaded at the time you execute the method. I guess it can be useful when you have loaded multiple scenes in additive mode, but personally I've never had to do that.

The name of that method is misleading, it should be named GetLoadedSceneByName.

The bad thing is, it seems there's still no way to retrieve a scene from the build settings by its name. And we're almost 2020.1!

avatar image AlternativeShit unity_HxXYq2JUbx4_8g · Mar 26, 2021 at 06:25 PM 0
Share

Thanks for commenting on this issue, I was actually stuck with it for a couple hours now... x) It's baffling to me that you can't just get a scene by name from the build settings...

avatar image
1

Answer by Bunny83 · Dec 12, 2015 at 05:01 PM

That makes no sense. If sceneToLoad is null (because there is no scene with that name), you would get a null reference exception at line 6 where you access the name of the returned scene object. So line 7 won't be executed if that's the case.

Since you said the scene does load that means GetSceneByName does return a valid Scene object.

What i could imagine what happens is that you have a static member variable also called sceneToLoad which of course isn't changed since you declare a new local variable in line 5. The variable "sceneToLoad" only exists inside the LoadScene method.

Comment
Add comment · Show 6 · 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 HarisKap · Dec 12, 2015 at 05:18 PM 0
Share

I agree with everything you said and thats true i didn't get a null reference exception. Just printed the debug message Null.

I am sure that isn't any static static member variable with that name.I also renamed to be sure.

The Scene$$anonymous$$anager.LoadScene(sceneName) works and what i believe is that Scene$$anonymous$$anager.GetSceneByName(sceneName); returns an empty scene object.

What i try to do is the following:

 private IEnumerator LoadLevelAsyncCoroutine(Scene scene)
     {
         AsyncOperation operation = Scene$$anonymous$$anager.LoadSceneAsync(scene.name);
 
         while(!operation.isDone) 
         {
             #if UNITY_EDITOR            
             Debug.Log("Loading Progress: " + operation.progress + " of " + scene.name);
             #endif
             LoadProgress = operation.progress;
 
             yield return null;
         }
 
         Debug.Log(scene.name + " Loaded. With build index: " + scene.buildIndex);
     }

But i can't do that if i can't get the scene by index or by name... Which i assumed that is a bug on the new scene manager of unity 5.3

If you loop through the active scenes has only the current one.

What i believe is happening is that they mixed the scenes system that you can add/ load on runtime (with the new system) with the building scenes and the documentation is not clear enough.

avatar image Bunny83 HarisKap · Dec 13, 2015 at 02:42 AM 0
Share

Are you sure you added your scene to the build settings? I haven't yet upgraded to 5.3 so i can't test it at the moment.

avatar image HarisKap Bunny83 · Dec 13, 2015 at 11:49 AM 0
Share

Yes, its not a new or small project. I just saw the update on 5.3 and i decided to refactor a bit the scene loading manager. But it seems it will wait for now.

avatar image Bunny83 HarisKap · Dec 13, 2015 at 12:41 PM 0
Share

Hmm, that's strange. Do you see the scene when you iterate through all scenes using Scene$$anonymous$$anager.GetAllScenes? Do you have tried creating a build or does this only happen in the editor when testing?

avatar image Andy Lee · Dec 21, 2015 at 06:30 AM 1
Share

I don't know the answer for this question yet, but for the Null Reference Exception.... no, you won't get the exception, because Scene is struct (value typed), it will never be null.

avatar image MasterHoover · Apr 05, 2016 at 02:05 AM 0
Share

It's not because the scene doesn't exists that GetSceneByName returns a null.

It all depends on how they made their function. They might return a scene made with a default constructor, with a default scene name and index.

I tested to see if the scene returned == null. And it doesn't.

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

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 is the new Scene object meant to be used? 1 Answer

How do I change scenes from triggers in UNity 2 Answers

Loading Screen? 6 Answers

Loading a level and getting all new gameobjects 1 Answer

Old Scene still visible after Loading new 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