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);
}

@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?

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.