Search Unity

If statements not being used

Discussion in 'Scripting' started by xrolliox, Feb 27, 2017.

  1. xrolliox

    xrolliox

    Joined:
    May 5, 2016
    Posts:
    55
    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):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class FoodItemManager : MonoBehaviour {
    7.  
    8.     public Text itemInfo;
    9.     public FoodClick food;
    10.     public ChopWood wood;
    11.     public float wcost, fcost, count, autoIncome;
    12.     public string upgradeName;
    13.     private float baseCost;
    14.     private float newCost;
    15.  
    16.  
    17.     // Use this for initialization
    18.     void Start () {
    19.         baseCost = wcost + fcost;
    20.         itemInfo.text = upgradeName + "\nFood Cost: " + fcost + "\nWood Cost: " + wcost + "\nFood/sec: " + autoIncome;
    21.     }
    22.    
    23.     // Update is called once per frame
    24.     void Update () {
    25.        
    26.     }
    27.  
    28.     public void PurchasedUpgrade()
    29.     {
    30.         if (wood.wood >= wcost)
    31.         {
    32.             if (food.food >= fcost)
    33.             {
    34.                 AllowPurchase();
    35.             }
    36.             else if (food.food <= fcost)
    37.             {
    38.                 return;
    39.             }
    40.             else if (wood.wood <= wcost)
    41.             {
    42.                 return;
    43.             }
    44.             }
    45.         }
    46.    
    47.  
    48.         void AllowPurchase()
    49.             {
    50.  
    51.             {
    52.                 wood.wood -= wcost;
    53.                 count += 1;
    54.                 food.food += autoIncome;
    55.                 wcost = Mathf.Round(baseCost * Mathf.Pow(1.70f, count));
    56.                 food.food -= fcost;
    57.                 fcost = Mathf.Round(baseCost * Mathf.Pow(1.30f, count));
    58.             }
    59.         }
    60.     }
    61.  
    62.  
    63.  
    As you can see it does check if the cost is more than the player has
    Code (CSharp):
    1. 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.
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    Code (CSharp):
    1. public void PurchasedUpgrade()
    2.     {
    3.         if (wood.wood >= wcost && food.food >= fcost)
    4.         {
    5.            AllowPurchase();
    6.         }
    7.      }
    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.
     
  3. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    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):
    1.  
    2. // bah pipped :P
    3.  
     
  4. xrolliox

    xrolliox

    Joined:
    May 5, 2016
    Posts:
    55
    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.
     
  5. xrolliox

    xrolliox

    Joined:
    May 5, 2016
    Posts:
    55
    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!