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

Resolved Issue with inventory UI

Discussion in 'Editor & General Support' started by TheanaProductions, Aug 26, 2021.

  1. TheanaProductions

    TheanaProductions

    Joined:
    Jul 3, 2021
    Posts:
    12
    Hi everyone,

    I'm currently working on an inventory system in order to improve my abilities in C# with Unity. It's far from being finished, but I currently have an issue with my UI... and absolutely no idea from where it comes.

    Basically, each time the player is collecting an item for the first time, it is added to a dictionary that serves me as a database for this particular player inventory. This dictionary contains an ItemSO as the key and an int as the value, which ends up being the amount of this item that the player has on him.

    Once it's in the dictionary, each time the player collects an item with the same name as one of the dictionary key, the value gets a +1 added to it.

    When I try it with 3 different items and get them in order (like, all 1st item, then all 2nd, etc.), it works perfectly and the right amount number is appearing. However, if I start to get them a little more randomly, the numbers aren't correct anymore. I've tried putting some debug.logs here and there, but they all return the good values, even though what's on the game screen isn't correct.

    So, in the end, my question is "simple", why isn't my object amount correct if they are picked randomly? And how can I fix it?

    And, while we're at it, if you see something that could be improved in this script in a general manner, I would be glad to hear it.

    Here is the script using the dictionary with the items:

    Code (CSharp):
    1. public class DatabaseSO : ScriptableObject
    2. {
    3.     public Dictionary<ItemSO, int> itemDictionary;
    4. }
    Code (CSharp):
    1.     [Header ("Inventory")]
    2.     [SerializeField] private List<GameObject> slots;
    3.     [SerializeField] private DatabaseSO database;
    4.    
    5.     [Header ("UI")]
    6.     [SerializeField] private GameObject prefabSlot;
    7.     [SerializeField] private GameObject parentObject;
    8.     [SerializeField] private GameObject inventoryUI;
    9.     private GameObject clone;
    10.  
    11.     private void Start()
    12.     {
    13.         slots = new List<GameObject>();
    14.         database.itemDictionary = new Dictionary<ItemSO, int>();
    15.     }
    16.  
    17.     public void AddItemDatabase(ItemSO itemSO)
    18.     {
    19.         if (database.itemDictionary.ContainsKey(itemSO))
    20.         {
    21.             UpdateUIAmount(itemSO);
    22.         }
    23.         else
    24.         {
    25.             database.itemDictionary.Add(itemSO, 0);
    26.             AddSlot(itemSO);
    27.         }
    28.     }
    29.  
    30.     public void AddSlot(ItemSO itemSO)
    31.     {
    32.         // Add a slot with the item each time you pick one
    33.         slots.Add(prefabSlot);
    34.  
    35.         clone = Instantiate(prefabSlot, parentObject.transform);
    36.  
    37.         Text nameTxt = clone.transform.Find("SlotTxt").GetComponent<Text>();
    38.         nameTxt.text = itemSO.nameItem;
    39.  
    40.         Image icon = clone.transform.Find("SlotIcon").GetComponent<Image>();
    41.         icon.sprite = itemSO.icon;
    42.  
    43.         UpdateUIAmount(itemSO);
    44.     }
    45.  
    46.     public void UpdateUIAmount(ItemSO itemSO)
    47.     {
    48.        int amountValue;
    49.  
    50.        database.itemDictionary[itemSO] += 1;
    51.        amountValue = database.itemDictionary[itemSO];
    52.  
    53.        Text amountTxt = clone.transform.Find("AmountTxt").GetComponent<Text>();
    54.        amountTxt.text = amountValue.ToString();
    55.  
    56.        Debug.Log("Amount updated: " + itemSO.nameItem + " " + database.itemDictionary[itemSO]);
    57.     }
    Thank you for your help!
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,962
    The main issue with the code above is going to be maintainability. You are conflating the actual tracking and processing of the data (counts, inventory, numbers, amounts, etc., aka "the model") with the presentation of the contents (the text, aka "the view" of your data).

    Break it all apart:

    - store and track the addition/removal of items, and add copious debugging into there to prove that all cases of adding/removing work perfectly (This should be ULTRA simple and tiny, just a little bookkeeper class)

    - create a UI (view) into this data and present it however you like.

    That way if in the future you radically change either one, they both don't just become a complete mess.

    Plus when you encounter a bug like the above, you have a much tinier area of code to consider: either prove that the error is in the bookkeeping section, or if that is spitting out correct values, look in the view.

    Beyond that, to help gain more insight into your problem, I recommend stripping things down to the barest simplest set of items that show off the problem, then liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?
    - are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

    Knowing this information will help you reason about the behavior you are seeing.

    You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494
     
  3. TheanaProductions

    TheanaProductions

    Joined:
    Jul 3, 2021
    Posts:
    12
    I see... Thank you, I'm going to try this tomorrow!