I want to make these scripts work in the scene: “Menu”. But it does not work, i have changed al the variables and code to let it work but it doesn’t work for me, maybe some body can help me with this problem.
I have this code from: Loading...
Thanks!
using UnityEngine;
using System.Collections;
public class LockLevel : MonoBehaviour {
public static int worlds = 1; //number of worlds
public static int levels = 8; //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);
}
}
}
}
}
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 == "Menu"+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");
}
}
}
}
using UnityEngine;
using System.Collections;
public class PlayerMovement : MonoBehaviour {
protected string currentLevel;
protected int worldIndex;
protected int levelIndex;
private string henk = "Het is ge";
// Use this for initialization
void Start () {
//save the current level name
currentLevel = Application.loadedLevelName;
}
// Update is called once per frame
void Update () {
transform.Translate(Input.GetAxis("Horizontal")*Time.deltaTime*10f, 0, 0); //get input
if(Input.anyKey)
{
UnlockLevels ();
print(henk);
}
}
protected void UnlockLevels (){
//set the playerprefs value of next level to 1 to unlock
for(int i = 0; i < LockLevel.worlds; 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);
}
}
}
//load the World1 level
Application.LoadLevel("Menu1");
}
}