Question by
Spirtax · Jan 18, 2021 at 10:56 AM ·
variablestower-defensemoneymoneysystemcurrency
Variable not decreasing when called,Need help with currency system..
void Update()
{
if (currentMoney < maxMoney && loseMoney == false)
{
moneyRateDecimal += Time.deltaTime * moneyRate;
currentMoney = Mathf.RoundToInt(moneyRateDecimal);
}
else
{
currentMoney -= 50;
loseMoney = false;
}
moneyText.text = "$" + currentMoney + " / $" + maxMoney;
}
public void onClick1()
{
if (currentMoney >= u1Cost)
{
Debug.Log("it works!");
SpawnUnit1();
loseMoney = true;
}
else
{
Debug.Log("Not enough money.");
}
}
Basically what I'm trying to do is create a tower defense like game. The money goes up by around 10 every second. I have a button that should be able to buy a unit and it should subtract the current amount of money I have by the cost of the unit. Instead it just keeps raising the amount of money I currently have and ignores where I tell it to subtract the cost. I've tried a lot of things like trying to subtract the money in a function and also trying an if/then statement in the void Update but nothing works.
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613023506im_/https://answers.unity.com/themes/thub/images/avi.jpg)