- Home /
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);
}
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.
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.'
Answer by $$anonymous$$ · Jan 20, 2016 at 08:22 PM
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?
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!
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...
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.
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.
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.
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.
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?
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.
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
Follow this Question
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