Inventory script resets when you pick up new item? [Solved]

Hey!

So I’m currently in a state of confused mixed with rage… I have a ‘working’ inventory system where if you pick up items (or chop a tree for instance) you get the items and it all works. So to easier explain this to you without posting the whole code my problem is if you already have say, 3 logs in the inventory and pick up a whole new item (Like a stone) logs reset to 1 and I get 1 stone.

I think I narrowed it down to 1 of 2 for loops but at this point, I’m going nuts and don’t understand why it resets

This is the one i use to update the UI.
UpdateUI

    void UpdateUI ()
    {
        // Loop through all the slots
        for (int i = 0; i < slots.Length; i++)
        {
            if (i < inventory.items.Count)    // If there is an item to add
            {
                slots[i].AddItem(inventory.items[i]);    // Add it
            }
            else
            {
                // Otherwise clear the slot
                slots[i].ClearSlot();
            }
        }
    }

or this one where I add the item to the inventory

Add(Item item)

public bool Add (Item item)
    {
        // Don't do anything if it's a default item
        if (!item.isDefaultItem)
        {
            if (items.Contains(item)) //if you have more then 1 in inv
            {

                for (int i = 0; i < slots.Length; i++)
                {
                    if (slots[i].ItemName == item.name)
                    {
                        slots[i].weight = slots[i].weight + 1;
                        slots[i].quantity.text = slots[i].weight.ToString();
                        return true;
                    }
                    else
                    {
                        Debug.Log("broke boi");
                    }
                }
            }
        if (items.Count >= space) //Out of space
        {
            Debug.Log("Not enough room.");
            return false;
        }
        items.Add(item);    // Add item to list
       return true;
}

Yes, code might not be most optimized but the goal is to make it work first :slight_smile: Need any more info just ask.

Thanks in advance!

Add additional debug statements and step through your logic Tips for new Unity users

Hey,
Yes. I’ve tried breakpoints and stepped through the whole thing. I think the problem is with the updateui. ama play around some more and see if I cant get more mad at it :slight_smile: Might try some battlecry or something and it might give me some hope!

Please share your updated code and the output and results of your debugging with the additional debug statements

1 Like

Jesus christ this took way to long then I want to admit heh… So I found the issue but it wasn’t really an issue but a forgotten line…

So I have 1 list with all the items but in the add method, I only updated the slots list (so there is 2 and I only updated the weight (Quantity) in one of them which caused this weird behavior…

The way I found out was to put Debug.Log(items*)* and just looked at what values wherein the items list vs the slots list. Silly mistake and took way too long heh.

Fixed code
[spoiler]
```csharp

  • for (int i = 0; i < slots.Length; i++)
    {
    if (slots[i].ItemName == item.name)
    {
    slots[i].weight = slots[i].weight + 1;
    items[i].Weight = items[i].Weight + 1; //This was the fix… Stupid me
    slots[i].quantity.text = slots[i].weight.ToString();
    return true;
    }
    else
    {
    Debug.Log(“Fel”);
    }
    }*
    ```
    [/spoiler]