Load currently loaded scene but add one to the name number
So I didn't know how to say this in the title, but I use this for my "reset level" button
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
Now I made a "next level" button where I want to change the name (my levels are named level1, level2, level3, etc.) into the same name but the number is one up, so for example if I'm on level4, if i press the button it will get me to level5. I have no idea how this could be done but I hope it's possible because I have 40+ levels to fix now and it's gonna be pain in the butt to go through them all and rename the strings. I also tried doing SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex++);
but that doesn't work, and If It did, my build index is all messed up so I have to change the actual name thanks in advance :)
Answer by srylain · Mar 19, 2016 at 06:17 AM
You could just have a script on a GameObject that isn't destroyed when a level loads, that keeps track of the level number. Then just reference that, add one to it, and load the next level.
Answer by tanoshimi · Mar 19, 2016 at 07:17 AM
The "good" solution would be to sort out your build index. Relying on string manipulation is always going to be fragile. Having said that, the following should work:
string currentLevelName = SceneManager.GetActiveScene().name;
string currentLevelNumberAsString = currentLevelName.Substring(5);
int currentLevelNumberAsInt;
if (int.TryParse(currentLevelNumberAsString , out currentLevelNumberAsInt)) {
SceneManager.LoadScene("Level" + (currentLevelNumberAsInt+1).ToString());
}
else
Debug.Log("Current level number could not be parsed.");
Your answer
Follow this Question
Related Questions
Why does my script and collider2D automatically deactivate after reloading the scene? 0 Answers
Asset Bundle Scene Loading Bug???? 0 Answers
How to fix Loading Screen? 0 Answers
Need Help with a Level Reloading Script (c#) 1 Answer
Variable Scene Loader 0 Answers