Load Next Level Error


If I get this error when I run code related to Next Level Load, do I need to create an additional index2 screen?

yes you do. Alternatively, you can just add an extra check inside the Next Level Load

if (currentLevel < 1) // to ensure we don't try to load level 2
{
  // only go hear if currentLevel is 0 to load level 1
}

An if-statement is a good idea. :slight_smile:

For more flexibility, one could even check against number of scenes:

if (currentLevel < SceneManager.sceneCountInBuildSettings)
{
    // code
}
    void LoadNextLevel()
    {   
        int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
        if (currentSceneIndex <= 1)   // to ensure we don't try to load level 2
        { 
            int nextSceneIndex = currentSceneIndex + 1;
            if (nextSceneIndex == SceneManager.sceneCountInBuildSettings) // "다음 씬 인덱스 = 우리가 가진 씬들의 총 개수"일 경우
            {
                nextSceneIndex = 0; // 씬 인덱스 초기화
            }
            SceneManager.LoadScene(nextSceneIndex);
        }
    
    }

The problem has been solved by changing the code as above. Thank you!

Good job! :slight_smile:

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

Privacy & Terms