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


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 :slight_smile:

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.

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());
    Debug.Log("Current level number could not be parsed.");