Scene index out of range exception

I’m not sure what I am missing. But suddenly I started getting ‘out of range’ exception for the scene index

        var availableCount = SceneManager.sceneCountInBuildSettings;
        var nextSceneIndex = SceneManager.GetActiveScene().buildIndex + 1;
        Debug.Log($"LoadNextLevel#1, cur = {SceneManager.GetActiveScene().buildIndex}, next = {nextSceneIndex}, max = {availableCount}");
        if (nextSceneIndex >= availableCount) {
            // back to the beginning
            nextSceneIndex = 0;
        }
        var scene = SceneManager.GetSceneAt(nextSceneIndex);
        Debug.Log($"GetSceneAt({nextSceneIndex}) = {scene.name}");
        SceneManager.LoadScene(nextSceneIndex);

There are 2 scenes in the scenemanager. I tried to remove and re-add the scenes, but I still get the same error.

image

The reason for adding the GetSceneAt was to see if I could get the scens, since the initial ‘loadscene’ does not load the next scene

        var availableCount = SceneManager.sceneCountInBuildSettings;
        var nextSceneIndex = SceneManager.GetActiveScene().buildIndex + 1;
        if (nextSceneIndex >= availableCount) {
            // back to the beginning
            nextSceneIndex = 0;
        }
        Debug.Log($"LoadNextLevel, cur = {SceneManager.GetActiveScene().buildIndex}, next = {nextSceneIndex}, max = {availableCount}");
        SceneManager.LoadScene(nextSceneIndex);

image

Any idea what I’m missing.

The problem fixed itself.
After some more tweaking the scenes (no change to code or scenemanager) then suddenly it did properly load the next scene. Really strange - but oh well, the issues is fixed

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms