I'm working on a platform game where you as a player have to collect logs, to keep on all the fires in the level. Each campfire has a timer, which ensures that every x number of seconds the animation of the fire is changed to a numb fire. The timer of a campfire is blocked by the script, when the player has received the fire at 100%. When all fires are 100% you won, if not you lose.

Know I use the following if statement to check if the player has won or lose:

if (Application.loadedLevelName == "Level 0") {

        if (campfireAmount == allCampfireFull) {
            // Do something
            print ("level 1 complete");
            Application.LoadLevel (6);

        if (campfireAmount == allCampfireEmpty) {
            // Do something
            print ("level 1 failed");
            //Application.LoadLevel ("GameOver");


What I searching for is how I can make this if statement useful for each level, whiteout use for each level scene above if statement. Beside that, what I want is that after each level you get a win scene (if possible a reusable scene) and after clicking in that scene on a button, you should go to the next level scene. Is there a way to load the next level, whiteout set a hard number of the rang list of "Build Settings"?

  • \$\begingroup\$ What do you mean by making the if-statement "useful for all levels"? Are you asking how to load the next level, regardless of which level you're currently at? \$\endgroup\$
    – Anko
    Apr 7, 2015 at 15:54
  • \$\begingroup\$ Yes exactly. @Anko \$\endgroup\$
    – Caspert
    Apr 7, 2015 at 15:58

2 Answers 2


You can load the "next" level with:

Application.LoadLevel(Application.loadedLevel + 1);

You will need to either:

  1. Setup the last level as a non-gameplay "You Win!" scene with a different exit condition or;
  2. Manually check you don't exceed Application.levelCount.

And you can count the number of objects with a given tag with:

int campFireCount = GameObject.FindGameObjectsWithTag("CampFire").Count();

If you update campFireCount in OnLevelWasLoaded() it should be easy to make a reusable script.

If you add the following line to your LoadScene script it will expose a public static int:

public static int nextLevelIndex;

You can then assign a value to this variable from anywhere, so you could include this line just before leaving a level:

LoadScene.nextLevelIndex = Application.loadedLevel + 1;

And then modify your loadScene() method to this:

public void loadScene() {
  • \$\begingroup\$ OK, that is good information to include in the question. You require communication between scripts and across scenes. Do you have a script in your win scene that loads the next gameplay level? If so the easiest way to pass a value would be a public static variable. \$\endgroup\$ Apr 7, 2015 at 15:53
  • \$\begingroup\$ I just notice, when you have won the second level, you see level three, but after that you see immediately the game over screen, because this one is the last one in the build settings. Is there a way to fix this. So if you reach last level and you win, the main menu will be displayed? \$\endgroup\$
    – Caspert
    Apr 8, 2015 at 20:26
  • \$\begingroup\$ Please see the numbered bullet points for a couple of options. \$\endgroup\$ Apr 8, 2015 at 22:49

if you use function pointers that are set outside of draw loop, you can use 0 if statements to act on functions for each level.

make a function that does what you need, and a pointer to that function, and swap out the function by a pointer based on what level you are on.

Pointerarray levelaction[10] for 10 levels using different functionality in draw loop

then just point them to the functions that are used for the level. and use the level as the index instead of a if condition.

note you get overhead of a function call, and don't get function inlined. But you get fewer ifs while having many levels.

switch statement is another option, where the compiler forms a binary tree to find the right case, making for fewer comparisons.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .