Using SceneManager.GetSceneByName.IsValid to find scenes in build

For some Scenes in my game I have 2 version, one “initial” version that is only used if it’s a new game. And one “_Load” that is used if the game is loaded from a save. Ex:

Initial Scene: “Village”
Scene in loaded game: “Village_Load”

In order to Load the correct Scene I created a method, but it seems “IsValid()” is not doing what I want. Since even if a Scene has a “_Load” version, it returns false (I always post the Initial Scene name as parameter):

public string GetSceneVersion(string scene)
    {
        if (newGame)
        {
            return scene;
        }
        else
        {
            if (!SceneManager.GetSceneByName(scene + "_Load").IsValid())
            {
                Debug.Log(scene + " only has one version");
                return scene;
            }
            else
            {
                Debug.Log(scene + " has a _Load version");
                return scene + "_Load";
            }
        }
    }

Is there some mistake here? Or does “IsValid()” Not find all Scenes in build? If not, what could I do instead?

I believe that will only return positive if the scene is also loaded, not just in the build settings.

You may have to keep track of valid scenes yourself, if some can have different names.

This is not the first time I’ve seen this… maybe someday there will be a ‘CheckForScene(string name)’.

2 Likes