error CS0119

Trying to get this inventory script to work but I’m having no luck

Error: Assets\Scripts\Inventory.cs(46,23): error CS0119: ‘Inventory.Slot’ is a type, which is not valid in the given context

Anyone can see the issue as I’ve been going through it for awhile now and I am not seeing it, I’m hoping someone with fresh eyes can see the mistake.

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

[System.Serializable]
public class Inventory
{
    [System.Serializable]
    public class Slot
    {
        public CollectableType type;
        public int count;
        public int maxAllowed;

        public Slot()
        {
            type = CollectableType.NONE;
            count = 0;
            maxAllowed = 99;
        }

        public bool CanAddItem()
        {
            if(count < maxAllowed)
            {
                return true;
            }

            return false;
        }

        public void AddItem(CollectableType type)
        {
            this.type = type;
            count++;
        }
    }

    public List<Slot> slots = new List<Slot> ();

    public Inventory(int numSlots)
    {
        for(int i = 0; i < numSlots; i++)
        {
            Slot slot = new Slot ();
            slots.Add(Slot);
        }
    }

    public void Add(CollectableType typeToAdd)
    {
        foreach(Slot slot in slots)
        {
            if(slot.type == typeToAdd && slot.CanAddItem())
            {
                slot.AddItem(typeToAdd);
                return;
            }
        }

        foreach(Slot slot in slots)
        {
            if(slot.type == CollectableType.NONE)
            {
                slot.AddItem(typeToAdd);
                return;
            }
        }
    }
}

Does it work if you take Slot outside of the class? Never had a use case for nested classes myself, so just throwing something

I tried, still got an error

You’ve accidentally capitalized “slot” on line 46.

1 Like

Thank you, that solved the issue. You’ve saved me.