Why works these scripts only with the Scene name World1?

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");
    }
}

Do you have Menu1 scene added to your build settings?

yes

Do you have any errors? Is anything working? Have you added Debug.Log to potential problem areas where you think it’s not working? Do you know how to use Monodevelops debugging features?

as in it doesn’t do what you want? what does it do?

It has no error, and it is a lock/unlock system, more info:Loading...

Have you tried increasing the number of worlds in the first script? Or it could be the fact that in your script, your menu needs to be named Menu1, Menu2 etc

I have done that , but it still doesn’t work

You need to put in Debug.Logs all throughout the code to narrow down exactly where it’s messing up. Which value isn’t changing to be what it’s supposed to be, where and when? Go every time a value is being modified and immediately afterward put a Debug.Log() that displays the value to the console. Tell us where the problem is, using this method, and we’ll be able to help you.

Also, if you’re creating strings to save to PlayerPrefs in this manner, I’d use string.Format() instead of what you’re doing with all of the quotes and stuff. That makes it far too easy to screw something up, especially if the result is important. For instance:

PlayerPrefs.SetInt(string.Format("level{0}-{1}", worldIndex, levelIndex),0);

(notice that you didn’t need to use .ToString(), it does it automatically), instead of

PlayerPrefs.SetInt("level"+worldIndex.ToString()+"-"+levelIndex.ToString(),0);

I have no idea how PlayerPrefs treats colons btw, so I changed it to a dash.

Ok, Lysander. So I put in some diffrent lines of code the Debug.Log(), and i will check it out, i hope i can find it because i’m a beginner in unity and c#

Wait, i found this error: Object reference not set to an instance of an object, at code line 37 in the levelselectscript.

GameObject.Find("LockedLevel"+(j+1)).active = false;

You’re reaching out to find something, and nothing is being found, so a better option might be:

GameObject someObject = GameObject.Find(string.Format("LockedLevel{0}", j+1));
if(someObject)
    someObject.SetActive(false);

So, problem 1, no object found. Problem 2, I’m not sure it actually formats (j+1) to a string automatically when you’re adding them with quotes that way (not sure), but I know that the string.Format function does. If you don’t want to use that, then say (j+1).ToString(), even if it looks a bit weird. Problem 3, “.active” is deprecated, so use “.SetActive()” instead.

It works, thank you man, i really appreciate it!