I have 9 levels in my games but when I run the game Unity keep increasing my scene index and said the error scene couldn't be loaded because it has not been added to the build settings. Here is the current error. It said scene 33 couldn't be loaded but I have only 9. Please help me how to fix this.Thank you
Here is my code for levels.I just increase 1 levels every time the player finish:
IEnumerator NextLevel()
{
finish = true;
PlayerPrefs.SetInt("Level", PlayerPrefs.GetInt("Level", 1) 1);
yield return new WaitForSeconds(1);
SceneManager.LoadScene("Level" PlayerPrefs.GetInt("Level"));
}
CodePudding user response:
You should make sure that the index to load is clamped to the actual levels available!
privte const int maxLevel = 9;
IEnumerator NextLevel()
{
finish = true;
var currentLevel = PlayerPrefs.GetInt("Level", 1);
var nextLevel = currentLevel 1;
if(nextLevel > maxLevel)
{
Debug.LogWarning(No more levels available);
yield break;
}
PlayerPrefs.SetInt("Level", nextLevel);
yield return new WaitForSeconds(1);
SceneManager.LoadScene(Level nextLevel.ToString());
}
CodePudding user response:
Assuming, you need to reset your PlayerPrefs after reading answer and all comments. @derHugo already answered how to delete a Key from PlayerPrefs. Additionally I want to add something -
You can now delete all keys in PlayerPrefs by simply Edit > Clear All PlayerPrefs
doing it.
Or, Use this tool to delete a specific key from the editor.