Level unlock system

Hey guys,

I am working on a level unlocking system for my iOS game. At this moment I got it working but I was wondering what you guys thought about my approach. I am asking you for feedback because I am fairly new to unity and programming in general and would very much like to improve my skills.
This is the code I have used. There is more in these scripts but to make it easier to read I have deleted some of the code that is not relevant to my question.

I have put this code on my first scene

#pragma strict

var numberOfLevels : int;

private var levels : int[];


function Start () {

levels = new int [numberOfLevels];
	for(var i :int = 1; i<levels.Length;i++){
		if (PlayerPrefs.GetInt("Level"+(i+1)) == null){
 			PlayerPrefs.SetInt("Level"+(i+1),0);
		}
	}

}

I have put the following code on a trigger at the end of all my levels:

#pragma strict
var numberOfLevels : int;


function Start () {
}

function Update () {
}

function OnTriggerEnter(collision : Collider){
NextLevel();
}

function NextLevel(){
UnlockLevel();
}

function UnlockLevel(){
for (var i : int = 1; i < numberOfLevels; i++){
	if(Application.loadedLevelName == "Level"+i){
	PlayerPrefs.SetInt("Level"+(i+1),1);
	}
}
}

And have put the following script on my levelselect menu:

#pragma strict
var numberOfLevelsWorld01 : int;


function Start () {
	for(var i : int = 1; i<numberOfLevelsWorld01; i++){
		if(PlayerPrefs.GetInt("Level"+(i+1))==1){
			GameObject.Find("Level"+(i+1)).GetComponent(BoxCollider).enabled= true;
			GameObject.Find("ButtonLocked"+(i+1)).active = false;
		}

	}
}

Thank in advance!
Kind regards,
Max Uijlings

Looks good to me. I like how you locked your level buttons. Might have to keep this in mind when I do something with levels.

Thanks good to hear I’m on the right track. :smile:

I did something real similar with my game. Looks good, and this will work well. Good thinking :slight_smile:

And then you may want to darken the unavailable levels so they look different or something.

Thanks for your feedback. At this moment I have a different texture representing the button being disabled. Would you say I should darken that out as well?

I would, but do what you think looks best :slight_smile:

oke thanks. I will experiment a little with it. I’ll let you know what I ended up with :slight_smile:

Kind Regards,
Max

I use a stageIndex and levelIndex of type int for each level and its managers.

PlayerPrefs.SetInt("levelUnlocked" + stageIndex.ToString() + levelIndex.ToString());
LevelUnlocked = PlayerPrefs.GetInt("levelUnlocked" + stageIndex.ToString() + levelIndex.ToString());

@renman3000 Thank you very much for this solution. It definitely made my code a lot cleaner. :slight_smile:

Kind Regards,
Max

I personally would have used PlayerPrefs.HasKey(“levelname”), because it returns a boolean… Also, you’ve got an empty update loop in there that should be removed :wink:

As pointed out by a fellow board member tho, I am going to modify it to be more like this, to prevent situation where stage 1 level index 11 don’t get confusd with stage index 11 level 1.

PlayerPrefs.SetInt("levelUnlocked" + stageIndex.ToString() + ":" + levelIndex.ToString());

hahahaha yeah good point I’ll change that right away.

Thanks for the advise. And indeed you are correct about the update function. I accidentally missed it. But off course I know that I should not have empty update functions in my code. :wink:

Oke guys so I changed the scripts to how I think it is good. I implemented your suggestions and ended up with the following.

In my first script at the beginning of the game I put the following code:

#pragma strict

static var worlds : int = 2;
static var levels : int = 18;

private var worldIndex : int;
private var levelIndex : int;


function Start () {
PlayerPrefs.DeleteAll();
LockLevels();
}


function LockLevels(){

	for (var i : int = 0; i < worlds; i++){
		for (var j : int = 1; j < levels; j++){
			worldIndex  = (i+1);
			levelIndex  = (j+1);
			if(!PlayerPrefs.HasKey("level"+worldIndex.ToString() +":" +levelIndex.ToString())){
				PlayerPrefs.SetInt("level"+worldIndex.ToString() +":" +levelIndex.ToString(),0);
			}
		
		}
	}

}

In the second script in my levelSelectMenu I have the following code:

#pragma strict


private var worldIndex : int;
private var levelIndex : int;

function Start () {
	for(var i : int = 1; i<=TempLevelSystem.worlds; i++){
		if(Application.loadedLevelName == "World"+i){
			worldIndex = i;
			CheckLockedLevels();

		}

	}

}

function CheckLockedLevels(){
	for(var j : int = 1; j<TempLevelSystem.levels; j++){
		levelIndex = (j+1);
		Debug.Log(""+worldIndex +levelIndex);
		if((PlayerPrefs.GetInt("level"+worldIndex.ToString() +":" +levelIndex.ToString()))==1){
			GameObject.Find("Level"+worldIndex.ToString() +"." +levelIndex.ToString()).GetComponent(BoxCollider).enabled= true;
			GameObject.Find("ButtonLocked"+(j+1)).active = false;
		}
	}
}

And the last script that will unlock the next level I put this code in:

#pragma strict
var level : String;
private var worldIndex : int;
private var levelIndex : int;
private var currentLevel : String;



function Start () {
currentLevel = Application.loadedLevelName;
}

function OnTriggerEnter(collision : Collider){
	NextLevel();
}

function NextLevel(){
	UnlockLevel();
	Application.LoadLevel(""+level);
}

function UnlockLevel(){
	for(var i : int = 0; i<TempLevelSystem.worlds; i++){
		for(var j : int = 1; j<TempLevelSystem.levels; j++){				
			if(currentLevel == "Level"+(i+1).ToString() +"." +j.ToString()){
				worldIndex  = (i+1);
				levelIndex  = (j+1);
				PlayerPrefs.SetInt("level"+worldIndex.ToString() +":" +levelIndex.ToString(),1);
			}
		}
	}
}

Again I have removed the code that is not relevant to this thread.

I hope you guys think I did a better job this time and that I have implemented your advice like you intended.
Your help has been very much appreciated. :smile:

Kind Regards,
Max Uijlings

I just discovered this-

First, you need to place this on an empty game object in your scene. It will provide 8 levels.
You also will need locked and unlocked icons going from 1-8.
Put this on the GameObject :

#pragma strict

var level1_locked:GameObject;
var level1_unlocked:GameObject;
var level2_locked:GameObject;
var level2_unlocked:GameObject;
var level3_locked:GameObject;
var level3_unlocked:GameObject;
var level4_locked:GameObject;
var level4_unlocked:GameObject;
var level5_locked:GameObject;
var level5_unlocked:GameObject;
var level6_locked:GameObject;
var level6_unlocked:GameObject;
var level7_locked:GameObject;
var level7_unlocked:GameObject;
var level8_locked:GameObject;
var level8_unlocked:GameObject;

var YouWin:String;

var levelReached1 : int = 0;

levelReached1 = PlayerPrefs.GetInt(“SavedLevel1”);

function Update(){

if(levelReached1 == 0)

{
level1_locked.active = false;
level1_unlocked.active = true;

level2_locked.active = true;
level2_unlocked.active = false;

level3_locked.active = true;
level3_unlocked.active = false;

level4_locked.active = true;
level4_unlocked.active = false;

level5_locked.active = true;
level5_unlocked.active = false;

level6_locked.active = true;
level6_unlocked.active = false;

level7_locked.active = true;
level7_unlocked.active = false;

level8_locked.active = true;
level8_unlocked.active = false;

}
if(levelReached1 == 1)
{
level1_locked.active = false;
level1_unlocked.active = true;

level2_locked.active = false;
level2_unlocked.active = true;

level3_locked.active = true;
level3_unlocked.active = false;

level4_locked.active = true;
level4_unlocked.active = false;

level5_locked.active = true;
level5_unlocked.active = false;

level6_locked.active = true;
level6_unlocked.active = false;

level7_locked.active = true;
level7_unlocked.active = false;

level8_locked.active = true;
level8_unlocked.active = false;

}
if(levelReached1 == 2)
{
level1_locked.active = false;
level1_unlocked.active = true;

level2_locked.active = false;
level2_unlocked.active = true;

level3_locked.active = false;
level3_unlocked.active = true;

level4_locked.active = true;
level4_unlocked.active = false;

level5_locked.active = true;
level5_unlocked.active = false;

level6_locked.active = true;
level6_unlocked.active = false;

level7_locked.active = true;
level7_unlocked.active = false;

level8_locked.active = true;
level8_unlocked.active = false;

}
if(levelReached1 == 3)
{
level1_locked.active = false;
level1_unlocked.active = true;

level2_locked.active = false;
level2_unlocked.active = true;

level3_locked.active = false;
level3_unlocked.active = true;

level4_locked.active = false;
level4_unlocked.active = true;

level5_locked.active = true;
level5_unlocked.active = false;

level6_locked.active = true;
level6_unlocked.active = false;

level7_locked.active = true;
level7_unlocked.active = false;

level8_locked.active = true;
level8_unlocked.active = false;

}
if(levelReached1 == 4)
{
level1_locked.active = false;
level1_unlocked.active = true;

level2_locked.active = false;
level2_unlocked.active = true;

level3_locked.active = false;
level3_unlocked.active = true;

level4_locked.active = false;
level4_unlocked.active = true;

level5_locked.active = false;
level5_unlocked.active = true;

level6_locked.active = true;
level6_unlocked.active = false;

level7_locked.active = true;
level7_unlocked.active = false;

level8_locked.active = true;
level8_unlocked.active = false;
}
if(levelReached1 == 5)
{
level1_locked.active = false;
level1_unlocked.active = true;

level2_locked.active = false;
level2_unlocked.active = true;

level3_locked.active = false;
level3_unlocked.active = true;

level4_locked.active = false;
level4_unlocked.active = true;

level5_locked.active = false;
level5_unlocked.active = true;

level6_locked.active = false;
level6_unlocked.active = true;

level7_locked.active = true;
level7_unlocked.active = false;

level8_locked.active = true;
level8_unlocked.active = false;
}
if(levelReached1 == 6)
{
level1_locked.active = false;
level1_unlocked.active = true;

level2_locked.active = false;
level2_unlocked.active = true;

level3_locked.active = false;
level3_unlocked.active = true;

level4_locked.active = false;
level4_unlocked.active = true;

level5_locked.active = false;
level5_unlocked.active = true;

level6_locked.active = false;
level6_unlocked.active = true;

level7_locked.active = false;
level7_unlocked.active = true;

level8_locked.active = true;
level8_unlocked.active = false;
}
if(levelReached1 == 7)
{
level1_locked.active = false;
level1_unlocked.active = true;

level2_locked.active = false;
level2_unlocked.active = true;

level3_locked.active = false;
level3_unlocked.active = true;

level4_locked.active = false;
level4_unlocked.active = true;

level5_locked.active = false;
level5_unlocked.active = true;

level6_locked.active = false;
level6_unlocked.active = true;

level7_locked.active = false;
level7_unlocked.active = true;

level8_locked.active = false;
level8_unlocked.active = true;
}
if(levelReached1 == 8)
{
Application.LoadLevel(YouWin);
}

}

Last of all you need to place this bit of code where you want to unlock a level.

PlayerPrefs.SetInt(“SavedLevel1”, 1);
Debug.Log(“SavedLeve1 = 1”);
yield WaitForSeconds(2.0);
//maybe have Application.LoadLevel(“Whatever level you want to load”);

do this with each level but change the places where it has the number 1 to 2 when you want to unlock level 2 and the same for 3-8.

I hope this helped.
Johnny ;D

I would recommend using arrays rather than numbers variables.

Good lord yes… how does that solution work with 255 levels?