Cannot load scene via SceneManager

Hello,

I tried to load scene using the scenemanager in Unity, but I can’t seem to be able to get it to load the next scene, either by name or by index.

Here is my build index:
3948976--338134--buildIndex.PNG

And what visual studio shows when I try to load the next scene:

I have seen many posts and have tried to build with all the scenes, without any of them and then building with them again. For some reason, the counts between the scenemanager and the build settings is different.

Can anyone help me?

The first is the number of the CURRENTLY LOADED scenes the second is the ‘how many scenes were added to the build settings’. So I don’t see any problems here.

ok then, If that is not the problem what could be the cause for it to not be able to load “Lvl2”?

post your code in code tags here, we wouldn’t know otherwise, how do you load it?

Here is the code I use:

void transitionToNextLevel()
    {
        if (levelPassed)
        {
            /** /
            string nextSceneName = lvlPrefix + SceneManager.GetActiveScene().buildIndex + 1;
            if (SceneManager.GetSceneByName(nextSceneName).IsValid())
            {

                SceneManager.LoadScene(nextSceneName);
                return;
            }
            /**/
            int nextSceneIndex = SceneManager.GetActiveScene().buildIndex + 1;
            if (SceneManager.GetSceneByBuildIndex(nextSceneIndex).IsValid())
            {

                SceneManager.LoadScene(nextSceneIndex);
                return;
            }
        }
      
        //either game passed or lost level
        SceneManager.LoadScene(0);
      

    }

Either one of the codes doesn’t work.

Read the manual.

Use [SceneManager.sceneCountInBuildSettings](https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager-sceneCountInBuildSettings.html) instead.

1 Like

Thanks for the help. It worked.

1 Like