GameManager issue on level load

Hello everyone,

So basically i am having a couple of issues with the levels of the game. So here is what’s happening:

I have a main menu of the game, it has the play, levels, and quit buttons.

Play: takes me to the first level of the scene.

Levels: takes me to the “Levels select” scene.

But here is the big problem. So when i start the first level from the “Play”, it begins level 1 and goes on to the other levels when 1 is completed.

But when i select a level from the “Levels Select” scene, let’s say level 5, and then i finish level five, it doesn’t take me to level 6 as it should, but rather takes me to level 2. I know the problem is from the GameManager, but i can’t seem to know the right function to fix this.

This is the GameManager:

using UnityEngine;
using System.Collections;

public class GameManager : MonoBehaviour {
    public static int currentScore;
    public static int highScore;

    public static int currentLevel = 0;


    public float startTime;
    private string currentTime;

    void Start()
    {
        DontDestroyOnLoad(gameObject);
    }

    public static void CompleteLevel()
    {
        if (currentLevel < 7)
        {
            currentLevel += 1;
            Application.LoadLevel(currentLevel);               
        } else {
            print ("Well done for completing the game, more levels coming soon ;)");
        }   
    }


}

And this is the LevelsSelect Scene:

var isLevelone=false;
var isLeveltwo=false;
var isLevelthree=false;
var isLevelfour=false;
var isLevelfive=false;
var isLevelsix=false;
var isLevelseven=false;
var isBack=false;

function OnMouseEnter(){
    //change text color
    renderer.material.color=Color.red;
}

function OnMouseExit(){
    //change text color
    renderer.material.color=Color.white;
}

function OnMouseUp(){
    //is this quit
    if (isLevelone==true) {
        Application.LoadLevel(0);
    }
   
    else if (isLeveltwo==true) {
        Application.LoadLevel(1);
    }
   
    else if (isLevelthree==true) {
        Application.LoadLevel(2);
    }
   
    else if (isLevelfour==true) {
        Application.LoadLevel(3);
    }
   
    else if (isLevelfive==true) {
        Application.LoadLevel(4);
    }
   
    else if (isLevelsix==true) {
        Application.LoadLevel(5);
    }
   
    else if(isLevelseven==true){
        Application.LoadLevel(6);
       
    }
   
    else if(isBack==true) {
        Application.LoadLevel(7);
    }
}

function Update(){
    //quit game if escape key is pressed
    if (Input.GetKey(KeyCode.Escape)){
             Application.Quit();
    }
}

Everytime you call ApplicationLoadLevel() you have to also update GameManager.currentLevel.
I think you could also get rid of all that boolean and if(isLevelxxx==true) just by setting currentLevel and calling Application.LoadLevel(GameManager.currentLevel).

I set up my GameManare class to have a function to load the level, and it calls Application.LoadLevel. This way it know when a level changes and it sets the current level, because it did the loading of it.

1 Like

Yes i do understand, but the problem is that i have different scenes, and the level select scene has its own script, while the GameManager has it’s own script, so how would Application.LoadLevel work if i have both scripts setup differently?

Thanks for the reply.

You pass in to the method what’s need for that specific scene.