Level Management

Hey there devs!I have a UI level selector menu for my 2d game but i run into an issue!When our player reaches for example lvl 2, and then clicks again on lvl1, his progress will be reseted but we do not want that.We want the button of lvl1, to be unclickable(not inactive or something) when the next level is reached!Do you have any info on what i should do or where i can find a solution in the unity learn section?I’m a intermediate unity user so i do not know much!Thanks in advance! :wink:

Hi there,

this has very little to do with UI itself. Check out PlayerPrefs in scripting manual!

I did but i do not seem to find anything that can help me(almost nothing) with my problem :stuck_out_tongue: .Any other ideas?

@SP Designs
Well, maybe you should grab some Unity game tutorial, I know this kind of stuff is bit hard to come by, I’m learning my self too, but I’d do it like this, if using PlayerPrefs:

  1. When player finishes level1, save it’s state to playerPrefs, something like: level1_completed with value 1.

  2. Next time you come back to menu, loop all your level buttons, check from playerPrefs if levelx_completed = 0, if so, keep it unlocked, else if completed = 1, disable it. This way you can keep books on solved levels, by checking all your levels againts what is (or isn’t) stored into playerPrefs.

I think i know how to do this but just one more question, if i make a script and put it on an empty game object in the menu scene will it still run?(loop through the level buttons?)

It will if you give it such as task.
I do this in my game as well with an ‘empty’ gameobject that sort of acts as a manager to all other scripts.

Cool thanks for the helpful reply :smile:!

Btw can someone help me write the code for the loop? I made an array to put my buttons in(is that right?).But how do i check if

Ok i set an int in player prefs for the completed_level but i am confused on how to write the loop code.I was thinking of maybe creating an array and then somehow loop through the buttons in the array?

Something like this

int currentlevel = PlayerPrefs.GetInt("CurrentLevel");
for (int i = 0; i < buttonsarray.length; i++){
    Button btn = buttonsarray[i];
    if(i == currentlevel){
        btn.interactable = true;
    } else {
        btn.interactable = false;
    }
}

Btw why do you ask the player to select the level if he has to choose the last one anyways.

You’re right actually.Maybe it would be better if i could find a script that won’t reset the players progress.
To be honest i just realized that the problem is that somehow the UnlockedLevel in playerprefs is being reseted to 1 if the player clicks level 1 again or any other level so he looses his progress.I have a script that runs when each level is passed but i think I’ve spotted the mistake.Here is the script :

 using UnityEngine;
using System.Collections;

publicclassLoadWorld1:MonoBehaviour{

publicintlevel_completed;

//publicintlevel_comp_to_save_with_index;

/*publicvoidLevelComplete_SetUnlockedLevel(intunlockedlevel)
{
unlockedlevel=level_completed;
if(PlayerPrefs.GetInt("UnlockedLevel")<unlockedlevel)
PlayerPrefs.SetInt("UnlockedLevel",unlockedlevel);
}*/


voidOnTriggerEnter2D(Collider2Dother){
PlayerPrefs.SetInt("UnlockedLevel",level_completed); --> This right here i believe is the mistake.So i need to make it so that this runs only the first time the player passes the levels and the next time the player replays the level it does not run this command.
/*if(other.gameObject.name=="Player"){
PlayerPrefs.SetInt("UnlockedLevel",level_completed);
}*/
//PlayerPrefs.SetInt("leveltodisable",level_comp_to_save_with_index);
Application.LoadLevel("World1");
}

}

So how can i check how many times the level has been played or otherwise how many times the PlayerPrefs.SetInt(…) command has run/?Any ideas?

EDIT : I actually fixed it with a for loop.For anyone who has the same problem this is what i used :

int for(i=0;i<1;i++){
PlayerPrefs.SetInt("UnlockedLevel",level_completed);
}

and here is the new code :

 using UnityEngine;
using System.Collections;

publicclassLoadWorld1:MonoBehaviour{

publicintlevel_completed;

voidOnTriggerEnter2D(Collider2Dother){
inti;
for(i=0;i<1;i++){
PlayerPrefs.SetInt("UnlockedLevel",level_completed);
}
Application.LoadLevel("World1");
}

}