How do I detect if a scene name is valid?

I’m setting up scripts to load scenes in the background. I want my loading script to verify that the scene it needs to load is valid in the start function rather than waiting until I try to load the scene.

I thought I could use scenemanager.getscenebyname and just see if that returns a null reference, but alas, it does not. It returns “an invalid scene” according to the documentation, and I don’t know how to check that.

How can I determine if a scene name is valid?

It’s not part of the scene manager but rather the EditorBuildSettings.

Specifically,

I think maybe you want this:

I’m not sure what it returns if the path is invalid. But maybe -1 or something? EditorBuildSettings is unfortunately editor only.

A quick test shows that I do indeed get a result of -1 if I feed it a false name.
I can use this! Thank you!

1 Like

Oh jeez, while I was tracking down how to find if a scene was already loaded I found this:

SceneManager.GetSceneByName(“SceneName”).isValid()

Well, I’ll just leave this here in case someone searches for this in the future.

2 Likes