I need help with a defenition error

Hey guys i got this error:

Assets\Scriptable Objects\Inventory\Scripts\InventoryObject.cs(22,29): error CS1061: InventorySlot[] does not contain a definition for Add and no accessible extension method Add accepting a first argument of type InventorySlot[] could be found (are you missing a using directive or an assembly reference?)

and been trying to fix it for a while now but i just cant do it any help would be appreciate,

Here’s my code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Runtime.Serialization.Formatters.Binary;
using UnityEngine.UIElements;
using System.IO;
using JetBrains.Annotations;

[CreateAssetMenu(fileName = "New Inventory", menuName = "Inventory System/Inventory")]
public class InventoryObject : ScriptableObject
{
    
  public string savePath;
  public ItemDatabaseObject database;
    public Inventory Container;
    

  public void AddItem(Item _item, int _amount)
    {
       
        if (_item.buffs.Length > 0) 
        {
            Container.Items.Add(new InventorySlot(_item.Id, _item, _amount));
            return;
        }

        for (int i = 0; i < Container.Items.Count; i++) 
        {
            if (Container.Items[i].item.Id == _item.Id)
            {
                Container.Items[i].AddAmount(_amount);
                return;
               
            }
        }
       Container.Items.Add(new InventorySlot(_item.Id,_item, _amount));
        
    }
    [ContextMenu("Save")]
    public void Save()
    {
        string saveData = JsonUtility.ToJson(this, true);
        BinaryFormatter bf = new BinaryFormatter();
        FileStream file = File.Create(string.Concat(Application.persistentDataPath, savePath));
        bf.Serialize(file, saveData);
        file.Close();

    }
    [ContextMenu("Load")]
    public void Load()
    {
        if(File.Exists(string.Concat(Application.persistentDataPath, savePath)))
        { 
            BinaryFormatter bf = new BinaryFormatter();
            FileStream file = File.Open(string.Concat(Application.persistentDataPath, savePath), FileMode.Open);
            JsonUtility.FromJsonOverwrite(bf.Deserialize(file).ToString(), this);
            file.Close();
        }    
    }
    [ContextMenu("Clear")]
    public void Clear()
    {
        Container = new Inventory();
    }
  
}
[System.Serializable]
public class Inventory
{
    //public List<InventorySlot> Items = new List<InventorySlot]>();
    public InventorySlot[] Items = new InventorySlot[24];
}
[System.Serializable]
public class InventorySlot
{
    public int ID;
    public Item item;
    public int amount;
    public InventorySlot(int _id, Item _item,int _amount)
    {
        ID = _id;   
        item = _item;
        amount = _amount;
    }
public void AddAmount(int value)
    {
        amount += value;
    }
}

The error message gives you the line and column number where the error occurs (in this case 22,29).

Container is of type Inventory and it appears to have some member variable named Items. But what is the type of Items? It would help to share the code for Inventory.

i think you mean this code, right?

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

public class DisplayInventory : MonoBehaviour
{
    public GameObject inventoryPrefab;
    public InventoryObject inventory;
    public int X_START;
    public int Y_START;
    public int X_SPACE_BETWEEN_ITEM;
    public int NUMBER_OF_COLUMN;
    public int Y_SPACE_BETWEEN_ITEMS;
    Dictionary<InventorySlot, GameObject> itemsDisplayed = new Dictionary<InventorySlot, GameObject>(); 
    
    void Start()
   {
       CreateDisplay();
    }

     
    void Update()
    {
        UpdateDisplay();
    }
    public void UpdateDisplay()
    {
        for(int i = 0; i < inventory.Container.Items.Count; i++)
        {
            InventorySlot slot = inventory.Container.Items[i];

            if (itemsDisplayed.ContainsKey(slot))
            {
                itemsDisplayed[slot].GetComponentInChildren<TextMeshProUGUI>().text = slot.amount.ToString("n0");
            }
            else
            {
                var obj = Instantiate(inventoryPrefab, Vector3.zero, Quaternion.identity, transform);
                obj.transform.GetChild(0).GetComponentInChildren<Image>().sprite = inventory.database.GetItem[slot.item.Id].uiDisplay;
                obj.GetComponent<RectTransform>().localPosition = GetPosition(i);
                obj.GetComponentInChildren<TextMeshProUGUI>().text = slot.amount.ToString("n0");
                itemsDisplayed.Add(slot, obj);
            }
        }
    }

    public void CreateDisplay()
    {
        for(int i = 0; i < inventory.Container.Items.Count; i++)
        {
            InventorySlot slot = inventory.Container.Items[i];
            var obj = Instantiate(inventoryPrefab, Vector3.zero, Quaternion.identity, transform);
            obj.transform.GetChild(0).GetComponentInChildren<Image>().sprite = inventory.database.GetItem[slot.item.Id].uiDisplay;
            obj.GetComponent<RectTransform>().localPosition = GetPosition(i);
            obj.GetComponentInChildren<TextMeshProUGUI>(). text = slot.amount.ToString("n0");
            itemsDisplayed.Add(slot, obj);
        }
    }
    public Vector3 GetPosition(int i)
    {
       return new Vector3(X_START + (X_SPACE_BETWEEN_ITEM * (i % NUMBER_OF_COLUMN)), Y_START+ (-Y_SPACE_BETWEEN_ITEMS * (i/NUMBER_OF_COLUMN)), 0f);
    }
}

or this

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

[CreateAssetMenu(fileName = "New Item Database", menuName ="Inventory System/Items/Database")]
public class ItemDatabaseObject : ScriptableObject, ISerializationCallbackReceiver
{
    public ItemObject[] Items;
    public Dictionary<int, ItemObject> GetItem = new Dictionary<int, ItemObject>();

    public void OnAfterDeserialize()
    {
        
        for (int i = 0; i < Items.Length; i++)
        {
            Items[i].Id = i;
            GetItem.Add(i, Items[i]);
        }
    }

    public void OnBeforeSerialize()
    {
        GetItem = new Dictionary<int, ItemObject>();
    }
}

Sorry, my mistake, I didn’t notice that the original code posted also included class Inventory.

public InventorySlot[] Items = new InventorySlot[24];

Items is an array. As far as I know, C# arrays don’t have an Add() method. Lists have an Add method, did you mean to use a List here?

If you need this to be an array for other reasons, then you may have to write your own function to add to the array in the Inventory class.

Inventories are pretty tricky since you may have empty slots. Lists are nice because you can add and remove elements without leaving any gaps. If you want to insert items into the first empty slot, perhaps you could do something like this:

public class Inventory
{
    //public List<InventorySlot> Items = new List<InventorySlot]>();
    public InventorySlot[] Items = new InventorySlot[24];

    public void AddItem(InventorySlot newItem)
    {
        bool success = false;
        //Try to find the lowest index that is null (empty)
        for (int i = 0; i < InventorySlot.Length; i++)
        {
            if (InventorySlot[i] == null)
            {
                InventorySlot[i] = newItem;
                success = true;
                break;
            }
        }
        if (!success)
        {
            Debug.Log("ERROR: Tried to insert item into a full inventory");
        }
    }
}

Then when you wanted to add something to the inventory, you would do:

Container.AddItem( <the thing you want to add>);

For error checking purposes, you could change AddItem to return the success bool and check that whenever you try to add an item to make sure it worked.