Hey Folks,
I have some questions and if some of you fellow devs can answer them great but it would really really great if someone from Unity could jump in too give some more in-depth information.
Afaik the “new” SceneManagement/SceneManager renders the old Level-Management obsolet which you also can see when you do some coding and the API tells you so ![]()
But how exactly can I get specific scenes I’d like to load? Either I’m not understanding it, the docs are not well documented on this matter (as is often the case, sadly ) or it just is not possible.
Scenario A)
I want to check if a specific scene exists in my build.
// **************************************************************************
#region Inspector Variables
public string sceneToLoad;
public LoadSceneMode loadSceneMode;
#endregion
[UsedImplicitly]
void Start()
{
if (!SceneManager.GetSceneByName(sceneToLoad).IsValid())
{
Debug.Log.Info("SceneToLoad wont be able to load since it is not in the buildsettings or is not valid");
}
}
When this script is attached to a gameobject in my scene, I’ll get the log right away when the scene starts ALTHOUGH the scene named by the string sceneToLoad does exist and is in the Buildsettings.
Now I was a bit confused. I understand that .GetSceneByName returns a sceneObject and not a bool, that’s why I used the .IsValid-Function to check if the returned scene is in somewhat form valid.
Since the log was printed it seems the scene wasn’t valid. That was the point I looked in the docs.
First of all: this way of documentation is terrible, since it is lacking information. You can’t state ONE example when something IsValid or not. What are other examples? List ALL of them, please.
Now after reading this I was as clueless as before. Then I looked at the .GetSceneByName-Method:
And I think I got it then. The SceneManager works differently than I thought. I knew that we can work with multiple scenes now in the Editor, but I didn’t realize that this makes actually a difference when checking for scenes. The Scene I want to check HAS to be added to the SceneManager first - meaning when speaking in Edit-Time the scene I want to check has to be added in the Hierarchy and only then .IsValid() can be true.
Now for the question: I looked at all methods of the SceneManager and apparently none gave me the possibility to check if a scene actually exists in my game/buildsettings. How can I check for that?
Scenario B) (which correlates with Scenario A)
This method here: SceneManager.CreateScene()
This method is dedicated to use at runtime, for EditScripting another method is suggested in the docs.
Now this method would be a case where I’d like to check if the created scene actually exists. How does the method work? Is the created scene added to the sceneCountInBuildSettings? Unlikely, since we’re at runtime.
Is it added to the sceneCount? Probably not.
In our game only one scene is loaded/active at a time so the sceneCount would ALWAYS be 1 in our case.
Like if I would use this method for some kind of procedural generated game (just assume we would like to use it, not if it’s good or bad practice)
- SceneManager.CreateScene() creates a scene and I add stuff to it.
- Player plays game, game saves at specific points.
- Player shuts off game
- Player starts game and wants to load that scene
→ how do I check for that specific scene?
I mean I know I could write everything manually, which is “no problem” per se - but is there actually a way within Unity to check for the existence of scenes?
Cheers