Search Unity

How to unlock the button?

Discussion in 'Scripting' started by polan31, Dec 22, 2019.

  1. polan31

    polan31

    Joined:
    Nov 20, 2017
    Posts:
    149
    Hello

    I created a simple pattern to unlock the button. When I get a certain number of items (1), the button is unlocked. Pressing the button consumes the item and "reloads" the button.
    I have two problems:
    1) after restarting the game from the menu, the button is loaded again instead of being locked or unlocked (depending on whether I have an item or not)
    2) when I use all the items, getting more will not unlock the button.

    I spent a few good nights on this problem, but nothing helps.
    The problem can be seen in the video below

    Video link:
    https://gfycat.com/pl/ordinarylegalannelida

    Can you help me solve this problem?


    I added this script to the item I get (on video-heart)

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. namespace CharacterUnlockSystem
    6. {
    7.     public class GetShield : MonoBehaviour
    8.     {
    9.         private void OnCollisionEnter2D(Collision2D collision)
    10.         {
    11.             if (collision.gameObject.CompareTag("Player"))
    12.             {
    13.                 ProgressionSystem.instance.addShieldItem();
    14.                 Destroy (this.gameObject);
    15.             }
    16.         }
    17.     }
    18. }

    I added these two scripts to the ButtonLockControl object, which allows me to control the unlocking of the button

    Code (CSharp):
    1. using System.Collections;
    2.     using System.Collections.Generic;
    3.     using UnityEngine;
    4.     using UnityEngine.UI;
    5.  
    6. namespace CharacterUnlockSystem
    7. {
    8.         public class FillAmountTest : MonoBehaviour
    9.         {
    10.  
    11.         [Header("Unlock requirements.")]
    12.  
    13.         public int Item1ItemRequired;
    14.  
    15.         progression _progression;
    16.  
    17.         // Reference to Lock Button Image
    18.         public Image lockImage;
    19.  
    20.         // Variable to hold Reload time value
    21.         // Can be set in Inspector
    22.         public float reloadTime = 5f;
    23.  
    24.         // Variable to hold time left value
    25.         // when weapon is reloaded
    26.         float timeLeft;
    27.  
    28.    
    29.         public static bool locked, reload;
    30.  
    31.  
    32.         void Start () {
    33.  
    34.             _progression = ProgressionSystem.instance.Progression;
    35.  
    36.             // Activate Lock Image at the beginning
    37.             lockImage.gameObject.SetActive (true);
    38.  
    39.             // Weapon is locked and reload is allowed at the beginning
    40.             locked = true;
    41.             reload = true;
    42.         }
    43.  
    44.         // Update is called once per frame
    45.         void Update () {
    46.  
    47.             // if fire ability is unlocked and reload is allowed
    48.             if (!locked && reload) {
    49.  
    50.                 // then reset time left value
    51.                 timeLeft = reloadTime;
    52.  
    53.                 // and start reload coroutine
    54.                 StartCoroutine ("Reload");
    55.  
    56.             }
    57.         }
    58.  
    59.         IEnumerator Reload()
    60.         {
    61.            reload = false;
    62.  
    63.             // Set Lock button image to active state
    64.             lockImage.gameObject.SetActive (true);
    65.  
    66.             // While time left is greater then 0
    67.             while (timeLeft > 0) {
    68.  
    69.                 // then set Image fill amount value
    70.                 // as time left devided by reload time
    71.                  
    72.                 lockImage.fillAmount = timeLeft/reloadTime;
    73.  
    74.                 // decrease time left value by Time.deltaTime value
    75.                 timeLeft -= Time.deltaTime;
    76.                 yield return null;
    77.      
    78.             }
    79.          
    80.             // Set Lock Image game object to inactive state
    81.             // when coroutine is finished
    82.             lockImage.gameObject.SetActive (false);
    83.  
    84.             if (_progression.shieldBTN < Item1ItemRequired)
    85.             {
    86.                 lockImage.gameObject.SetActive (true);
    87.                 lockImage.fillAmount = 1;
    88.         }
    89.          
    90.     }
    91.  
    92.     }
    93. }
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. namespace CharacterUnlockSystem
    6. {
    7. public class ShieldCounter : MonoBehaviour
    8. {
    9.     // Start is called before the first frame update
    10.  
    11.     [Header("Unlock requirements.")]
    12.  
    13.     public int Item1ItemRequired;
    14.  
    15.     progression _progression;
    16.     ProgressionSystem ps;
    17.     // Reference to kills counter text game object
    18.  
    19.     // Use this for initialization
    20.     void Start () {
    21.         _progression = ProgressionSystem.instance.Progression;
    22.  
    23.     }
    24.  
    25.     // Update is called once per frame
    26.     void Update () {
    27.  
    28.         // Unlock Fire weapon if kills number greater then 5
    29.         if (_progression.shieldBTN >= Item1ItemRequired){
    30.              
    31.             FillAmountTest.locked = false;
    32.  
    33.         }
    34.             else if (_progression.shieldBTN < Item1ItemRequired)
    35.             {
    36.                            FillAmountTest.locked = true;
    37.  
    38.             }
    39.         }
    40.  
    41.     }
    42. }
     
    Last edited: Dec 23, 2019
  2. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    Hi,

    Maybe format your code a bit better for readability?
    https://forum.unity.com/threads/using-code-tags-properly.143875/

    But if you need to have some value / state persist game reboot, you can save your value to player prefs or some file, and then reload it when game starts, and set your objects based on that value.