Adventure Game Inventory (Unity Tutorial) increasing amount of slots

Hey, I just followed the tutorial from Unity Adventure Game. Now I’d like to make some more slots and add my own stuff into this.
I have added some more slots in the Inventory (using prefabs like in the video) and increased the amount of numItemSlots.
The problem is that items get updated until 4th slot and the item that should be on the 5th ovewrites 4th slot again and again.

Edit: It have to be something with the added Slots. I just disabled one that i created while watching the tutorial and now it stops at the 3rd

Can you share your updated, relevant code?

This is my Inventory script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Inventory : MonoBehaviour
{
    public const int numItemSlots = 7; //

    public Image[] itemImages = new Image[numItemSlots];
    public Item[] items = new Item[numItemSlots];


    public void AddItem(Item itemToAdd)
    {
        for (int i = 0; i < items.Length; i++)
        {
            if(items[i] == null)
            {
                //Debug.Log(i);
                items[i] = itemToAdd;
                itemImages[i].sprite = itemToAdd.sprite;
                itemImages[i].enabled = true;
                return;
            }
        }
    }

    public void RemoveItem(Item itemToRemove)
    {
        for (int i = 0; i < items.Length; i++)
        {
            if(items[i] == itemToRemove)
            {
                items[i] = null;
                itemImages[i].sprite = null;
                itemImages[i].enabled = false;
                return;
            }
        }
    }

}

just in case:

InventoryEditor

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

[CustomEditor(typeof(Inventory))]

public class InventoryEditor : Editor
{
    private bool[] showItemSlots = new bool[Inventory.numItemSlots];
    private SerializedProperty itemImagesProperty;
    private SerializedProperty itemsProperty;
    private const string inventoryPropItemImagesName = "itemImages";
    private const string inventoryPropItemsName = "items";


    private void OnEnable()
    {
        itemImagesProperty = serializedObject.FindProperty(inventoryPropItemImagesName);
        itemsProperty = serializedObject.FindProperty(inventoryPropItemsName);
    }

    public override void OnInspectorGUI()
    {
        serializedObject.Update();

        itemImagesProperty.arraySize = Inventory.numItemSlots;
        itemsProperty.arraySize = Inventory.numItemSlots;

        for (int i = 0; i < Inventory.numItemSlots; i++)
        {
            ItemSlotGUI(i);
        }

        serializedObject.ApplyModifiedProperties();
    }

    private void ItemSlotGUI(int index)
    {
        EditorGUILayout.BeginVertical(GUI.skin.box);
        EditorGUI.indentLevel++;

        showItemSlots[index] = EditorGUILayout.Foldout(showItemSlots[index], "Item slot " + index);

        if(showItemSlots[index])
        {
            EditorGUILayout.PropertyField(itemImagesProperty.GetArrayElementAtIndex(index)); // <-- ein fehler!?
            EditorGUILayout.PropertyField(itemsProperty.GetArrayElementAtIndex(index));
        }

        EditorGUI.indentLevel--;
        EditorGUILayout.EndVertical();
    }
}

and my itemscript

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PickupItem : MonoBehaviour
{
    public string itemID;
    public Item itemdata;

    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
       
    }

    void OnTriggerEnter2D(Collider2D other)
    {
        if(other.tag == "Player")
        {
            Inventory inventory = FindObjectOfType<Inventory>();
            inventory.AddItem(itemdata);
            Destroy(gameObject);
        }
    }
}

It looks fine. My best guess is that you’ve changed the value of the ItemSlots constant from 4 to 7, but your Items and ItemImages arrays serialized with the original value of 4, so it’s not respecting your changed value. I would try removing the Inventory component from your object and re-adding it and see if that fixes it.

1 Like

After re-adding inventory doesn’t work completly. I get an error by running through items:

NullReferenceException: Object reference not set to an instance of an object
Inventory.AddItem (Item itemToAdd) (at Assets/Inventory/Inventory.cs:22)
PickupItem.OnTriggerEnter2D (UnityEngine.Collider2D other) (at Assets/Inventory/PickupItem.cs:27)

Anything that I need to change in Inventory script now? Or is it the item? But still a step fowards now I’ve an error to work with.

It’s saying something related to itemImages or itemToAdd is null – did you re-setup your canvas UI images in the appropriate slots?

1 Like

Oh sure I totally forgot it to set it up. Works perfetly now thank you very much!:slight_smile:

1 Like

No problem, glad I didn’t permanently break your project. :slight_smile:

Best of luck!

1 Like