I have a problem with the number of coins when I have 0 coins my number of coins goes negative
I would buy only if I had the cost
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class HandleBasket : MonoBehaviour
{
public int cost = 0;
private Image image;
private Button button;
private bool enable = false;
private CoinCounter coinCounter;
void Start()
{
image = GetComponent<Image>();
button = GetComponent<Button>();
coinCounter = GameObject.Find("CoinCount").GetComponent<CoinCounter>();
enable = PlayerPrefs.GetInt(gameObject.tag, 0) == 1;
}
void Update()
{
Color c = image.color;
if (enable)
{
c = Color.yellow;
}
else
{
if (coinCounter.coinCount >= cost)
{
c.a = 1;
}
else
{
c.a = 0.1f;
}
}
image.color = c;
}
public void buy()
{
if (enable)
{
return;
}
coinCounter.coinCount -= cost;
enable = true;
}
void OnDestroy()
{
PlayerPrefs.SetInt(gameObject.tag, enable ? 1 : 0);
PlayerPrefs.Save();
}
}