How to go to next level dynamically with SceneManager.

I’ve been searching for a dynamic solution for loading the next level using SceneManager. There does not appear to be any such solution, but in case I’ve missed something, I’ll post this here.

I need something similar to this:

void OnTriggerEnter(Collider col)
{
	if (col.gameObject.tag == "Player") 
	{
		int thisLevel = Application.loadedLevel;
		Application.LoadLevel (thisLevel + 1);
	}
}

But in the SceneManager format.

You need this line at the top of your script:

using UnityEngine.SceneManagement;

and use SceneManager instead of Loadlevel

SceneManager.LoadScene("Scene Name");

This should work:

using UnityEngine.SceneManagement;

void OnTriggerEnter(Collider col)
 {
     if (col.gameObject.tag == "Player") 
     {
         int thisLevel = SceneManager.GetActiveScene().buildIndex;
         SceneManager.LoadScene(thisLevel + 1);
     }
 }