A more systemic approach to switching levels

This is how I went about loading levels on death and on win.

   int currentLevel; // create an integer variable for storing the current scene index

then in start:

        currentLevel = SceneManager.GetActiveScene().buildIndex;  // get the active scene's index and store it in an integer variable

and then we use the collision switch like this

       switch(collision.gameObject.tag)
        {
            case "Friendly":
                //TODO Might add fuel or something later
                break;
            case "Finish":
                currentLevel = ++currentLevel; // increase the current level integer by one
                print(currentLevel);
                SceneManager.LoadScene(currentLevel); // load the next level, which is current level increased by 1
                break;
            case "FinalPad":
                print("You have completed the game!");
                break;
            default:
                print("DEAD!");
                SceneManager.LoadScene(currentLevel);
                break;
        }

That way you can add lots of new levels without changing the code. You just need to make sure your final level’s landing pad has a tag called “FinalPad”

6 Likes

This fantastic. I was hoping to learn how to advance scenes soon, and this makes quick work of the process. I haven’t reached the final courseware that covers this topic yet, but I’m glad to get a sneak peak at a fellow student’s work, so I can explore with my project. :slight_smile: Thanks!

1 Like

I was trying to get something similar working but couldn’t work out how to obtain the current scene index and store it in an integer, so thanks for this!

I did something similar, but with two differences:

  1. I also considered the fact that landing on the last scene’s pad would cause an error when loading a next scene that isn’t there
  2. I extracted it to a utility class (this class has other things in it, but i removed those to show the code related to this)
public enum GameAction { Start, Failed, Completed, Finished };

public class Utilities : MonoBehaviour {
	public static void SmartSceneLoader(GameAction gameAction) {
		Scene activeScene = SceneManager.GetActiveScene();
		int sceneCount = SceneManager.sceneCountInBuildSettings;

		switch (gameAction) {
			case GameAction.Completed:
				if (activeScene.buildIndex == sceneCount - 1) {
					print("No more scenes");
					break;
				}
				SceneManager.LoadScene(activeScene.buildIndex + 1);
				break;
			case GameAction.Failed:
				SceneManager.LoadScene(activeScene.buildIndex);
				break;
			default:
				break;
		}
	}
}

Usage:


	void OnCollisionEnter(Collision collision) {
		string colliderTag = collision.gameObject.tag;

		switch (colliderTag) {
			case "Friendly":
				break;
			case "Finish":
				Utilities.SmartSceneLoader(GameAction.Completed);
				break;
			default:
				Utilities.SmartSceneLoader(GameAction.Failed);
				break;
		}
	}
1 Like

Privacy & Terms