Ramses Beau Bams
Ramses Beau Bams

Reputation: 55

Unlock levels in Unity3D

I'd like to make a system that unlock the next level after completing one. I'm using c# to code

If you complete level 1, you unlock level2 etc...

mainmenu is build level 0 and the level select is build level 1, so the first level is actually build level 2.

public bool isLevel1;
public bool isLevel2;
public bool isLevel3;
public bool isLevel4;


// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {

}
void OnMouseUp()
{
    if (isLevel1)
    {
        Application.LoadLevel(2);
    }
    if (isLevel2)
    {
        Application.LoadLevel(3);
    }
    if (isLevel3)
    {
        Application.LoadLevel(4);
    }
    if (isLevel4)
    {
        Application.LoadLevel(5);
    }
}

that's how I select the levels from the level select. i had something similar on the mainmenu, but I don't have a clue how to lock them and then unlock then after completing a level. I had a way to lock them, but i abandoned that since it didn't work. it'd be a big help if somebody could explain it simple enough or point me to a guide that i missed. because i found a few guides, but they assumed i did my level seleciton different, i think. I thank you in advance and i'm willing to answer any further questions

Upvotes: 0

Views: 2070

Answers (2)

bpgeck
bpgeck

Reputation: 1624

Maybe include a script on some empty game object (make sure you call Object.DontDestroyOnLoad(transform.gameObject) so the object remains when levels change), and each time you complete a level, change a boolean variable in that empty object to signify that the level has been completed at least once. Then, unlock the next level in your UI if the boolean signifying the previous level has been completed is true.

If your character automatically advances to the next level after completing the previous one you could instead use OnLevelWasLoaded(int level). level in this case is the number of the level that was just loaded (so for you 3 would be your real first level). And then change the boolean variables of the empty game object in this function instead of at the end of the previous level. This could be most useful if, when you finish the level, there are different levels you could advance to next (there isn't just one option to unlock).

Upvotes: 0

Tom
Tom

Reputation: 2472

I would suggest that PlayerPrefs is probably the best bet for doing something like this, it works well for saving scores for the player so saving unlocked levels could work too.

Here's some documentation for PlayerPrefs: http://docs.unity3d.com/ScriptReference/PlayerPrefs.html

And here is a good video link showing similar to what you want to achieve :) https://www.youtube.com/watch?v=uw0kZ72zCvE&feature=youtu.be

Upvotes: 2

Related Questions