- Home /
Problem to go to another world scene in level select lock/unlock
Hello everyone,
http://www.thegamecontriver.com/2014/09/create-level-lock-unlock-system-unity-46.html#comment-form
I´m follow this tutorial to make a Level lock/unlock system to my game and it´s works but when i finish the levels in the first world, i can´t change/go to the second world scene. My script it´s very similar to the tutorial.
Thank you. The codes it´s here:
My scenes have name: the worlds to select levels:
World1 World2
Levels scenes:
Level1.1 Level1.2 .. Level2.1 Level2.2
first script:
using UnityEngine; using System.Collections;
public class LockLevel : MonoBehaviour {
public static int worlds = 2; //number of worlds
public static int levels = 3; //number of levels
private int worldIndex;
private int levelIndex;
void Start (){
PlayerPrefs.DeleteAll(); //erase data on start
LockLevels(); //call function LockLevels
}
//function to lock the levels
void LockLevels (){
//loop thorugh all the levels of all the worlds
for (int i = 0; i < worlds; i++){
for (int j = 1; j < levels; j++){
worldIndex = (i+1);
levelIndex = (j+1);
//create a PlayerPrefs of that particular level and world and set it's to 0, if no key of that name exists
if(!PlayerPrefs.HasKey("level"+worldIndex.ToString() +":" +levelIndex.ToString())){ PlayerPrefs.SetInt("level"+worldIndex.ToString() +":" +levelIndex.ToString(),0); }
} } } }
Second Script:
using UnityEngine; using System.Collections;
public class LevelSelectScript : MonoBehaviour {
private int worldIndex;
private int levelIndex;
void Start (){
//loop thorugh all the worlds
for(int i = 1; i <= LockLevel.worlds; i++){
if(Application.loadedLevelName == "World"+i){
worldIndex = i;
CheckLockedLevels();
}
}
}
//Level to load on button click. Will be used for Level button click event
public void Selectlevel(string worldLevel){
Application.LoadLevel("Level"+worldLevel); //load the level
}
//uncomment the below code if you have a main menu scene to navigate to it on clicking escape when in World1 scene
/*public void Update (){
if (Input.GetKeyDown(KeyCode.Escape) ){ Application.LoadLevel("MainMenu"); }
}*/
//function to check for the levels locked
void CheckLockedLevels (){
//loop through the levels of a particular world
for(int j = 1; j < LockLevel.levels; j++){
levelIndex = (j+1);
if((PlayerPrefs.GetInt("level"+worldIndex.ToString() +":" +levelIndex.ToString()))==1){
GameObject.Find("LockedLevel"+(j+1)).active = false;
Debug.Log ("Unlocked");
}
}
}
}
Third script: the player part when he comes to the goal:
protected void UnlockLevels (){ //set the playerprefs value of next level to 1 to unlock for(int i = 0; i < LockLevel.worlds; i++){
Debug.Log(i);
for(int j = 1; j < LockLevel.levels; j++){
if(currentLevel == "Level"+(i+1).ToString() +"." +j.ToString()){
worldIndex = (i+1);
levelIndex = (j+1);
PlayerPrefs.SetInt("level"+worldIndex.ToString() +":" +levelIndex.ToString(),1);
}
}
}
Application.LoadLevel("World1");
//I try Application.LoadLevel("World1"+((worldIndex+1).ToString)); but doesn´t work
}
Your answer
Follow this Question
Related Questions
How to have GearVR play smooth during LoadLevelAsync 0 Answers
linking levels? 2 Answers
Can I utilize LoadLevelAdditive for immersive game? 2 Answers