Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Bug need help with inventory system

Discussion in 'Scripting' started by bernardwalrus44, May 19, 2024.

  1. bernardwalrus44

    bernardwalrus44

    Joined:
    Feb 25, 2024
    Posts:
    3


    I've been following this video and for some reason the items wont display in my inventory, but they have been picked up as they show up in my items array if any one knows how to fix this that would be greatly apricated.
    code below.




    Code (CSharp):
    1. public class InventoryManager : MonoBehaviour
    2. {
    3.     public static InventoryManager instance;
    4.     public List<item> items = new List<item>();
    5.     public Transform content;
    6.     public GameObject inventoryitem;
    7.     private void Awake()
    8.     {
    9.         instance = this;
    10.     }
    11.  
    12.     public void add(item item)
    13.     {
    14.         items.Add(item);
    15.  
    16.  
    17.     }
    18.  
    19.  
    20.     public void remove(item item)
    21.     {
    22.         items.Remove(item);
    23.  
    24.  
    25.  
    26.     }
    27.  
    28.     public void listitems()
    29.     {
    30.         foreach (var item in items)
    31.         {
    32.             GameObject obj = Instantiate(inventoryitem, content);
    33.  
    34.             var itemName = obj.transform.Find("ItemName").GetComponent<TMPro.TextMeshProUGUI>();
    35.             var itemIcon = obj.transform.Find("itemIcon").GetComponent<Image>();
    36.             itemName.text = item.ItemName;
    37.             itemIcon.sprite = item.icon;
    38.  
    39.         }
    40.  
    41.  
    42.     }
    43.  
    44.  
    45.  
     
  2. bugfinders

    bugfinders

    Joined:
    Jul 5, 2018
    Posts:
    2,190
    are you sure you set icons?
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,388
    This sounds like a bug, perhaps a mistake in how you did the tutorial, perhaps something else.

    Whatever the cause it doesn't matter: it's on you to figure it out and fix it.

    The way you figure out and fix a bug is by debugging.

    That means it is time to begin your first debugging adventure!

    By debugging you can find out exactly what your program is doing so you can fix it.

    https://docs.unity3d.com/Manual/ManagedCodeDebugging.html

    Use the above techniques to get the information you need in order to reason about what the problem is.

    You can also use
    Debug.Log(...);
    statements to find out if any of your code is even running. Don't assume it is.

    Once you understand what the problem is, you may begin to reason about a solution to the problem.
     
    bernardwalrus44 likes this.
  4. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,709
    I see considerably more code in the video than I do in that snippet you've posted. If I had to guess you haven't written the code that calls
    ListItems
    . You're not following it exactly either. I see different capitalization in your methods. Little differences like this mean the tutorial may break in ways that are difficult to detect.

    Speaking of the code seeing this in a loop just makes me cringe. It's at least one of the faster variants of
    Find
    but the author never should have had it in the loop. The
    Find
    methods are extremely brittle and if you use it like this everywhere performance will be just awful.
     
    Last edited: May 19, 2024
    Ohilo and mopthrow like this.