Rpg Development Tutorials, Guides & Insights
Unlock 1+ expert-curated rpg tutorials, real-world code snippets, and modern dev strategies. From fundamentals to advanced topics, boost your rpg skills on DeveloperBreeze.
Adblocker Detected
It looks like you're using an adblocker. Our website relies on ads to keep running. Please consider disabling your adblocker to support us and access the content.
Unity Inventory System using Scriptable Objects
Code August 12, 2024
csharp
A simple inventory system that can add, remove, and use items.
using System.Collections.Generic;
using UnityEngine;
public class Inventory : MonoBehaviour
{
public List<Item> items = new List<Item>();
public int capacity = 20;
public bool AddItem(Item item)
{
if (items.Count >= capacity)
{
Debug.Log("Inventory is full!");
return false;
}
if (item.isStackable)
{
Item existingItem = items.Find(i => i.itemName == item.itemName);
if (existingItem != null)
{
// Stack logic (if needed)
Debug.Log($"Stacking {item.itemName}");
return true;
}
}
items.Add(item);
Debug.Log($"{item.itemName} added to inventory.");
return true;
}
public void RemoveItem(Item item)
{
if (items.Contains(item))
{
items.Remove(item);
Debug.Log($"{item.itemName} removed from inventory.");
}
}
public void UseItem(Item item)
{
if (items.Contains(item))
{
item.Use();
}
}
}