Been trying to make a list of items that can be unlocked on button click. Basically, clicking on the button shows the item underneath it. At the moment, I only put four items just to test things out. However, for some reason, I am having the following issues:
- When I click an item, it’s always item2 that gets unlocked.
- When I try clicking the other items, the button does not disappear.
- When I exit the shop and return to it, all the items are suddenly unlocked.
This is the code I have for buying an item, and is currently attached to the button itself.
public void UnlockItem()
{
if(DataManager.Instance.currency >= 20)
{
button.gameObject.SetActive(false);
DataManager.Instance.currency -= 20;
DataManager.Instance.unlockStatus = true;
DataManager.Instance.Save();
Debug.Log("ITEM SHOWN");
}
}
I’ve also tried attaching the script to an empty game object, but it’s still not working as intended. Do the items need a unique ID for this to work? I would appreciate some help on this.
EDIT: Here’s the DataManager code. To clarify, the “item” is really just an image with some text, and is covered by a button. When the button is clicked, the player is able to see the image underneath. That’s it.
public class DataManager : MonoBehaviour
{
public static DataManager Instance;
public int score;
public int currency;
public bool unlockStatus;
private void Awake()
{
if (Instance != null && Instance != this)
{
Destroy(gameObject);
}
else
Instance = this;
DontDestroyOnLoad(gameObject);
Load();
}
[System.Serializable]
class SaveData
{
public int score;
public int currency;
public bool unlockStatus;
}
public void Save()
{
SaveData data = new SaveData();
data.score = score;
data.currency = currency;
data.unlockStatus = unlockStatus;
string json = JsonUtility.ToJson(data);
File.WriteAllText(Application.persistentDataPath + "/savefile.json", json);
}
public void Load()
{
string path = Application.persistentDataPath + "/savefile.json";
if (File.Exists(path))
{
string json = File.ReadAllText(path);
SaveData data = JsonUtility.FromJson<SaveData>(json);
score = data.score;
currency = data.currency;
unlockStatus = data.unlockStatus;
}
}
}