Singular Inventory Items

I have a script, that I need the player to drag a specific inventory item to it… how would I get that singular item in a script… let’s say I have a script that uses a wood Item per hour… how would I get that inventory Item in script by name. And use it for tests if it’s there or not?.

Put a field in your reducing script InventoryItem fuel;

Depending on if you’ve taken the Shops and Abilities course, you may need to add a method to Inventory.cs:

        public bool RemoveItem(InventoryItem item)
        {
            int index = FindItemByName(item.name);
            if (index > -1)
            {
                Debug.Log($"Removing {item.name} from slot {index}");
                slots[index].number =0;
                slots[index].item = null;
                inventoryUpdated?.Invoke();
                return true;
            }
            else
            {
                   return false;
            }
        }      

When it’s time to reduce a log, test Inventory.RemoveItem(fuel); If it’s true, reset the timer. If it’s false, put out the fire

1 Like

Thank you, I will get to trying to right away

There should be a method within Inventory called HasItem.

        public bool HasItem(InventoryItem item)
        {
            for (int i = 0; i < slots.Length; i++)
            {
                if (object.ReferenceEquals(slots[i].item, item))
                {
                    return true;
                }
            }
            return false;
        }

Sorry , I wasn’t clear. I mean I made an inventory item called wood. , How can I single it out by name , so that the script can take that item from the inventory at runtime. And fill on it’s slot.

[SerializeField] InventoryItem wood; //drag the wood Inventory Item here

Then it will stay , even in runtime . Oh sorry , it was sos simple

Privacy & Terms