How to go to next level

Hi,
I have a simple game with just 1 scene and 1 XML. On the start of the scene I look at the XML where I have the level structure.
My issues is now how to change to next level, I’ll need to pass the number of level to the scene. Also I tried Application.LoadLevel (but it’s says is deprecated). How can I do it?

Thanks

using UnityEngine.SceneManagement;

public class Example
{
    public void LoadLevel()
{
        // load the nextlevel
  SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);

    }
}

Be sure to add them in File>Build Settings

4 Likes

Thanks for you reply…
That will load the next scene in the list, right?
What if I need the same scene but the level variable increased by 1?

Yes

Then load the same scene, but increase the variable and Handle whatever it does on the load, or via a Singleton.
But thats why scenes are there for, idk why you would want to do this.

Or perhaps I may be misunderstanding you.

If I am loading all levels in a single scene then
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1); will not works.

2 Likes

Need some changes,
Use this code :
private void Start()
{
nextLevel = SceneManager.GetActiveScene().buildIndex + 1;
}
void OnTriggerEnter2D(Collider2D collision)
{
// Application.LoadLevel(loadLevel);
SceneManager.LoadScene(nextLevel);

}

It´s a post from 3 years ago.
Furthermore, what you posted is the same + an OnTriggerEnter2D that has nothing to do with what OP asked. Perhaps you posted on the wrong thread?

thanks for the help i really needed that because i am an beginner :slight_smile:

If this didn’t work then you didn’t go to File>>Build_Settings>>Add_Open_Scenes

You will need your scenes to be in the proper order inside the build list unless you switch scenes by scene name reference rather than build index.