How to use GetActiveScene to change scenes

I’m doing a Unity tutorial course on a simple platformer. I want to change to the next level when touching the goal, reset the current level when touching an enemy, and when finishing the fourth (last) level, I want to loop[ back to the first one.

The tutorial used an int in the GameManager, currentLevel, to keep track of what scene is loaded. This stopped working at a later point and the currentLevel would keep being reloaded as 1 because the GameManager would reset on every scene change (even with DontDestroyOnLoad in the GameManager).

After some googling, I saw people recommending GetActiveScene.buildIndex to figure the current scene out and change it. I implemented this, but Unity tells me GetActiveScene has to be in Awake or Start. When I put it there, the two ints (tracking current and next scene index) that use GetActiveScene.buildIndex stop working in a later method where the level is changed.

How do I use GetActiveScene? And if this is a unnecessarily complicated way to change scenes, how do people usually go about doing that?

I recommend using Scene Names instead of the build index. You could throw all of your scene names into a string array and just loop through them

using UnityEngine.SceneManagement;

string sceneName;
sceneName = SceneManager.GetActiveScene().name;