Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Lock/Unlock system don't work perfectly

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

  1. Barft

    Barft

    Joined:
    Apr 9, 2015
    Posts:
    84
    Hi all, for my lock/unlock system I have used this tutorial because I'm a beginner in unity, http://www.thegamecontriver.com/2014/09/create-level-lock-unlock-system-unity-46.html

    I did everything the same, but only the last script (Player Movement), in my game you got for example 2 holes and 2 bales, in every hole must go a ball to load to next level and unlock the level. This is the script:

    Code (CSharp):
    1. public bool[] holeTrigger = new bool[6];
    2.  
    3. protected string currentLevel;
    4. protected int worldIndex;
    5. protected int levelIndex;
    6. // Use this for initialization
    7. void Start () {
    8.      //save the current level name
    9.      currentLevel = Application.loadedLevelName;
    10. }
    11. // Update is called once per frame
    12. void Update () {
    13.      //move the player based on left and right arrow keys
    14.      transform.Translate(Input.GetAxis("Horizontal")*Time.deltaTime*10f, 0, 0); //get input
    15. }
    16. public void TriggerHole(int index) {
    17.      holeTrigger[index] = true; //Set the hole trigger to true
    18.    
    19.      //Check if all holes have been triggered
    20.      if (AllTriggered)    
    21.          UnlockLevels();   //unlock next level funxtion
    22.          Application.LoadLevel("fLevel1");
    23. }
    24. //This method will iterate through each of the triggers in the array and return false if any of them are not true.
    25. public bool AllTriggered {
    26.      get{
    27.          bool b = true;
    28.          foreach(bool bX in holeTrigger){
    29.              if(!bX)
    30.                  b = false;
    31.          }
    32.          return b;
    33.          ;}
    34. }
    35. protected void  UnlockLevels (){
    36.      //set the playerprefs value of next level to 1 to unlock
    37.      for(int i = 0; i < LockLevel.worlds; i++){
    38.          for(int j = 1; j < LockLevel.levels; j++){              
    39.              if(currentLevel == "Level"+(i+1).ToString() +"." +j.ToString()){
    40.                  worldIndex  = (i+1);
    41.                  levelIndex  = (j+1);
    42.                  PlayerPrefs.SetInt("level"+worldIndex.ToString() +":" +levelIndex.ToString(),1);
    43.              }
    44.          }
    45.      }
    46. }
    So when the balls are in the two holes it will load "fLevel1" and unlock the next level, but the problem is that it loads and unlock the next level when there is one ball in one hole, but the purpose is that there are two balls and two holes and unlock and load the next level when they are in the holes. If I delete the UnlockLevel(); it works. Does somebody know what the problem is?

    Thanks!
     
  2. lordconstant

    lordconstant

    Joined:
    Jul 4, 2013
    Posts:
    389
    Your alltriggered bool doesnt need the get{} surrounding it. Also you need to add braces for the if(AllTriggered ) line as right now whenever trigger hole gets called it will load a level. Without braces it only covers the next line after the statement.
     
    Barft likes this.
  3. Barft

    Barft

    Joined:
    Apr 9, 2015
    Posts:
    84
    Ow thats really stupid of me, it works now. Thank you really really much!