Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Money Not Adding to Inventory

Discussion in '2D' started by jretchford, Apr 21, 2020.

  1. jretchford

    jretchford

    Joined:
    Dec 28, 2019
    Posts:
    3
    Hello to whomever's reading this.

    I'm new to Unity, so I'll try explaining this as best as I can. I'm creating a 2D game, in which as you can tell from the title has a money system that I'm trying to create. I've set up an inventory system in the UI, and also set up when you click the money on screen it disappears. However, for some reason the money doesn't get added to the inventory total.

    Here's the code for the inventory system, you may have to ignore some aspects that I'm yet to put in place like a shirt and a book. however here it is:

    Code (CSharp):
    1. public class InventorySystem : MonoBehaviour
    2. {
    3.  
    4.     public Image shirt;
    5.     public Image book;
    6.  
    7.     public int money;
    8.  
    9.     public TextMeshProUGUI moneyDisplay;
    10.  
    11.     public float timer;
    12.  
    13.     public List<string> collectedItems;
    14.  
    15.     public AudioSource collectMoneySound;
    16.     public AudioSource collectItemSound;
    17.  
    18.     private void FixedUpdate()
    19.     {
    20.         moneyDisplay.text = money.ToString();
    21.  
    22.         timer += Time.deltaTime;
    23.  
    24.     }
    25.  
    26.     private void Awake()
    27.     {
    28.         DontDestroyOnLoad(gameObject);
    29.  
    30.         shirt.gameObject.SetActive(false);
    31.         book.gameObject.SetActive(false);
    32.     }
    33.  
    34.  
    35. }
    and here is also the code for what happens when you click on the money in the game:

    Code (CSharp):
    1. {
    2.     public void Collect()
    3.     {
    4.         gameObject.SetActive(false);
    5.  
    6.         FindObjectOfType<InventorySystem>().money += 50;
    7.  
    8.         FindObjectOfType<InventorySystem>().collectedItems.Add(gameObject.name);
    9.  
    10.         FindObjectOfType<InventorySystem>().collectMoneySound.Play();
    11.     }
    12.  
    13.     private void Awake()
    14.     {
    15.         if (FindObjectOfType<InventorySystem>().collectedItems.Contains(gameObject.name))
    16.         {
    17.             gameObject.SetActive(false);
    18.         }
    19.     }
    20. }

    Again, all help would be appreciated, as I previously said, I'm new to this, so apologies if the solution is obvious and simple.

    Many thanks!
     
  2. GrayLightGames

    GrayLightGames

    Joined:
    Jan 14, 2015
    Posts:
    30
    Hi jretchford,

    First off, let me confirm, is the second block your collectible object class?

    You may want to look into referencing the InventorySystem object with a variable instead of using FindObjectOfType... or at least calling FindObjectOfType once in Awake() and storing the result in a variable.

    So I see a number of possible ways the money would not update:
    1. Collect() is not actually being called.
    2. FindObjectOfType<InventorySystem>() is not finding the right object.
    3. The money is being updated but it's not showing on the screen properly.

    I would use Debug.Log statements to find out this info... you can add this to the top line of Collect():

    Code (CSharp):
    1. Debug.Log("COLLECT CALLED");
    You should see that message (COLLECT CALLED) in your console if it is being called. You can also use this method to find if the correct InventorySystem is being retrieved (I'll assume you're capturing the IS in a variable called inventorySystem):

    Code (CSharp):
    1. Debug.Log("IS Name: " + inventorySystem.gameObject.name + " Money: " + inventorySystem.money);
    That should put a message in the console with the name of your IS and the amount. Then you can check the money at the start of the function and the end to make sure it's being added.

    You don't need to update the moneyDisplay.text in FixedUpdate, you can just update it whenever it changes. Any other time, it's a wasted operation. So I'd add a function to your IS called UpdateMoney() that updates the Text from the value, and then you can just call that function any time the money changes, in your Collect() function for example.

    Feel free to let me know if I haven't made things clear or if you figure out what's going wrong...

    Hope that helps, hang in there!
     
  3. jretchford

    jretchford

    Joined:
    Dec 28, 2019
    Posts:
    3
    Thank you, this helps a lot.

    think I just got a few things muddled up, but I’ll check it out soon. Many thanks once again