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): using System.Collections; using System.Collections.Generic; using UnityEngine; namespace CharacterUnlockSystem { public class GetShield : MonoBehaviour { private void OnCollisionEnter2D(Collision2D collision) { if (collision.gameObject.CompareTag("Player")) { ProgressionSystem.instance.addShieldItem(); Destroy (this.gameObject); } } } } I added these two scripts to the ButtonLockControl object, which allows me to control the unlocking of the button Code (CSharp): using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; namespace CharacterUnlockSystem { public class FillAmountTest : MonoBehaviour { [Header("Unlock requirements.")] public int Item1ItemRequired; progression _progression; // Reference to Lock Button Image public Image lockImage; // Variable to hold Reload time value // Can be set in Inspector public float reloadTime = 5f; // Variable to hold time left value // when weapon is reloaded float timeLeft; public static bool locked, reload; void Start () { _progression = ProgressionSystem.instance.Progression; // Activate Lock Image at the beginning lockImage.gameObject.SetActive (true); // Weapon is locked and reload is allowed at the beginning locked = true; reload = true; } // Update is called once per frame void Update () { // if fire ability is unlocked and reload is allowed if (!locked && reload) { // then reset time left value timeLeft = reloadTime; // and start reload coroutine StartCoroutine ("Reload"); } } IEnumerator Reload() { reload = false; // Set Lock button image to active state lockImage.gameObject.SetActive (true); // While time left is greater then 0 while (timeLeft > 0) { // then set Image fill amount value // as time left devided by reload time lockImage.fillAmount = timeLeft/reloadTime; // decrease time left value by Time.deltaTime value timeLeft -= Time.deltaTime; yield return null; } // Set Lock Image game object to inactive state // when coroutine is finished lockImage.gameObject.SetActive (false); if (_progression.shieldBTN < Item1ItemRequired) { lockImage.gameObject.SetActive (true); lockImage.fillAmount = 1; } } } } Code (CSharp): using System.Collections; using System.Collections.Generic; using UnityEngine; namespace CharacterUnlockSystem { public class ShieldCounter : MonoBehaviour { // Start is called before the first frame update [Header("Unlock requirements.")] public int Item1ItemRequired; progression _progression; ProgressionSystem ps; // Reference to kills counter text game object // Use this for initialization void Start () { _progression = ProgressionSystem.instance.Progression; } // Update is called once per frame void Update () { // Unlock Fire weapon if kills number greater then 5 if (_progression.shieldBTN >= Item1ItemRequired){ FillAmountTest.locked = false; } else if (_progression.shieldBTN < Item1ItemRequired) { FillAmountTest.locked = true; } } } }
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.