Hi Guys, I've just come back to this code after a week or so to get some fresh eyes. I initially had a problem with Auto Deducting resources (a problem I have now solved in one of the other scripts.) However, one problem still remains. I have 3 resources that the player uses (Wood, Food, Money) They use wood to purchase a research item "Food" and it unlocks the Food system. But purchasing food requires both food and wood. Before I went away the OnPurchase function would simply not deduct any of the resources, and would just allow the upgrade to be purchased at no cost. After playing with the code I have got to the point below. This piece of code however, will deduct both food and wood costs. Even if the players food count is 0. Code (CSharp): using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class FoodItemManager : MonoBehaviour { public Text itemInfo; public FoodClick food; public ChopWood wood; public float wcost, fcost, count, autoIncome; public string upgradeName; private float baseCost; private float newCost; // Use this for initialization void Start () { baseCost = wcost + fcost; itemInfo.text = upgradeName + "\nFood Cost: " + fcost + "\nWood Cost: " + wcost + "\nFood/sec: " + autoIncome; } // Update is called once per frame void Update () { } public void PurchasedUpgrade() { if (wood.wood >= wcost) { if (food.food >= fcost) { AllowPurchase(); } else if (food.food <= fcost) { return; } else if (wood.wood <= wcost) { return; } } } void AllowPurchase() { { wood.wood -= wcost; count += 1; food.food += autoIncome; wcost = Mathf.Round(baseCost * Mathf.Pow(1.70f, count)); food.food -= fcost; fcost = Mathf.Round(baseCost * Mathf.Pow(1.30f, count)); } } } As you can see it does check if the cost is more than the player has Code (CSharp): else if (food.food <= fcost) {return} But it still allows the purchase anyway. (I want to allow the player to go into a negative amount [to go 'bankrupt'] but I don't want to allow them to purchase things if they don't have enough money.
Code (CSharp): public void PurchasedUpgrade() { if (wood.wood >= wcost && food.food >= fcost) { AllowPurchase(); } } This should be all you need unless you want to also warn them of what they are low on, then you can use else statements to check if it's wood or food they are low on to display a message. Next, if it's still triggering, you need to make sure all your values are correct.
lines 36 to 44 are irrelevant, they are else clauses that dont do anything and there isn't any further code to complete in that function anyway... in fact you could just combine it to Code (csharp): // bah pipped :P
Funnily enough this is what I had when I left the code. I'll see about putting that back in, and seeing if it works this way now.
Okay, it works now! The problem I was having before was that the "AllowPurchase" function was simply following after the if statements. But now I've put it into a function that is called if, the if statements are true it works. I just have to go implement this on my further tech's. I'll come back if I run into any more problems. Thanky you!