Search Unity

Why works these scripts only with the Scene name World1?

Discussion in 'Scripting' started by Barft, Jul 27, 2015.

  1. Barft

    Barft

    Joined:
    Apr 9, 2015
    Posts:
    84
    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: http://www.thegamecontriver.com/2014/09/create-level-lock-unlock-system-unity-46.html

    Thanks!

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class LockLevel : MonoBehaviour {
    5.    
    6.    
    7.     public static int worlds = 1; //number of worlds
    8.     public static int levels = 8; //number of levels
    9.    
    10.     private int worldIndex;  
    11.     private int levelIndex;  
    12.    
    13.    
    14.     void  Start (){
    15.         PlayerPrefs.DeleteAll(); //erase data on start
    16.         LockLevels();   //call function LockLevels
    17.     }
    18.    
    19.     //function to lock the levels
    20.     void  LockLevels (){
    21.         //loop thorugh all the levels of all the worlds
    22.         for (int i = 0; i < worlds; i++){
    23.             for (int j = 1; j < levels; j++){
    24.                 worldIndex  = (i+1);
    25.                 levelIndex  = (j+1);
    26.                 //create a PlayerPrefs of that particular level and world and set it's to 0, if no key of that name exists
    27.                 if(!PlayerPrefs.HasKey("level"+worldIndex.ToString() +":" +levelIndex.ToString())){
    28.                     PlayerPrefs.SetInt("level"+worldIndex.ToString() +":" +levelIndex.ToString(),0);
    29.                 }
    30.                
    31.             }
    32.         }
    33.        
    34.     }
    35. }
    36.  
    37.  
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class LevelSelectScript : MonoBehaviour {
    5.    
    6.     private int worldIndex;  
    7.     private int levelIndex;  
    8.    
    9.     void  Start (){
    10.         //loop thorugh all the worlds
    11.         for(int i = 1; i <= LockLevel.worlds; i++){
    12.             if(Application.loadedLevelName == "Menu"+i){
    13.                 worldIndex = i;
    14.                 CheckLockedLevels();
    15.             }
    16.         }
    17.     }
    18.    
    19.     //Level to load on button click. Will be used for Level button click event
    20.     public void Selectlevel(string worldLevel){
    21.         Application.LoadLevel("Level"+worldLevel); //load the level
    22.     }
    23.    
    24.     //uncomment the below code if you have a main menu scene to navigate to it on clicking escape when in World1 scene
    25.     /*public void  Update (){
    26.   if (Input.GetKeyDown(KeyCode.Escape) ){
    27.    Application.LoadLevel("MainMenu");
    28.   }  
    29. }*/
    30.    
    31.     //function to check for the levels locked
    32.     void  CheckLockedLevels (){
    33.         //loop through the levels of a particular world
    34.         for(int j = 1; j < LockLevel.levels; j++){
    35.             levelIndex = (j+1);
    36.             if((PlayerPrefs.GetInt("level"+worldIndex.ToString() +":" +levelIndex.ToString()))==1){
    37.                 GameObject.Find("LockedLevel"+(j+1)).active = false;
    38.                 Debug.Log ("Unlocked");
    39.             }
    40.         }
    41.     }
    42. }
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerMovement : MonoBehaviour {
    5.  
    6.     protected string currentLevel;
    7.     protected int worldIndex;
    8.     protected int levelIndex;
    9.  
    10.     private string henk = "Het is ge";
    11.  
    12.     // Use this for initialization
    13.     void Start () {
    14.         //save the current level name
    15.         currentLevel = Application.loadedLevelName;
    16.     }
    17.  
    18.    
    19.     // Update is called once per frame
    20.     void Update () {
    21.    
    22.         transform.Translate(Input.GetAxis("Horizontal")*Time.deltaTime*10f, 0, 0); //get input
    23.  
    24.         if(Input.anyKey)
    25.         {
    26.             UnlockLevels ();
    27.  
    28.             print(henk);
    29.         }
    30.  
    31.     }
    32.  
    33.     protected void  UnlockLevels (){
    34.         //set the playerprefs value of next level to 1 to unlock
    35.         for(int i = 0; i < LockLevel.worlds; i++){
    36.             for(int j = 1; j < LockLevel.levels; j++){              
    37.                 if(currentLevel == "Level"+(i+1).ToString() +"." +j.ToString()){
    38.                     worldIndex  = (i+1);
    39.                     levelIndex  = (j+1);
    40.                     PlayerPrefs.SetInt("level"+worldIndex.ToString() +":" +levelIndex.ToString(),1);
    41.                 }
    42.             }
    43.         }
    44.         //load the World1 level
    45.         Application.LoadLevel("Menu1");
    46.     }
    47. }
    48.  
     
  2. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483
    Do you have Menu1 scene added to your build settings?
     
  3. Barft

    Barft

    Joined:
    Apr 9, 2015
    Posts:
    84
  4. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483
    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?
     
  5. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    as in it doesn't do what you want? what does it do?
     
  6. Barft

    Barft

    Joined:
    Apr 9, 2015
    Posts:
    84
  7. Cnc96

    Cnc96

    Joined:
    Dec 17, 2013
    Posts:
    57
    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
     
  8. Barft

    Barft

    Joined:
    Apr 9, 2015
    Posts:
    84
    I have done that , but it still doesn't work
     
  9. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    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:
    Code (csharp):
    1. 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
    Code (csharp):
    1. PlayerPrefs.SetInt("level"+worldIndex.ToString()+"-"+levelIndex.ToString(),0);
    I have no idea how PlayerPrefs treats colons btw, so I changed it to a dash.
     
  10. Barft

    Barft

    Joined:
    Apr 9, 2015
    Posts:
    84
    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#
     
  11. Barft

    Barft

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

    Code (CSharp):
    1. GameObject.Find("LockedLevel"+(j+1)).active = false;
     
  12. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    You're reaching out to find something, and nothing is being found, so a better option might be:
    Code (csharp):
    1. GameObject someObject = GameObject.Find(string.Format("LockedLevel{0}", j+1));
    2. if(someObject)
    3.     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.
     
  13. Barft

    Barft

    Joined:
    Apr 9, 2015
    Posts:
    84
    It works, thank you man, i really appreciate it!