I have seen a lot of tutorials out there on creating inventory systems, but they are all using static databases of static items. I want to have randomly generated stats for my items, so that approach doesn’t seem to work too well. I’ve had a couple of ideas for how to approach it, but just curious if anyone knows of any proven approaches for doing so?
My initial idea was to instantiate a prefab for each item when you enter a shop or whatever. At start, it will randomly generate the stats for that item. If you purchase/pickup the item, it will make the item a child of an Inventory parent item in the scene, with sprite renderer, and other components that are unnecessary in an unequipped state disabled. Then when you equip it, it moves it becomes the child of an equip slot instead. That way the random attributes of each item are preserved. This way seems easiest to me, but I wonder how it would work resource-wise. Especially if you are fighting enemies that also have inventories etc.
The only other idea I have is to have an inventory script that stores item details in a two dimensional relational array, so for example:
Inventory[0][Type] = Gun,
Inventory[0][Name] = Laser Cannon,
Inventory[0][Damage] = 50
Etc… And then when you equip the item, it will instantiate it in the scene. I would think this would be more resource friendly, but would it be enough so for the extra work?
1. My first piece of advice would be, to build at first an inventory system according to this Unity Inventory Tutorial. This is a really solid basis and gives you a lot of things you need e.g. your own type “Item” with customisable preferences and stats (keyword: "ScriptableObject). All these preferences are not static and you can easily generate random items.
I use this inventory system approach my self.
2. If you really need multiple true inventories, create yourself a parent class for this, for example:
public abstract class Inventory : Monobehavior { ... }
And all your inventories for the enemies, player, … are inherit from this parent class “Inventory”, like
public class Enemy : Inventory {...}
For further background information about inheritance in C# and Unity see for example this nice tutorial.
3. To handle single items in a scene I distinguish two cases:
Case One: Items that are in my inventory consist only of two types: the mentioned Item type (consists of multiple types like int Amount, string Name, sprite, …) and an int for the amount in my inventory. And both are stored in arrays (one for each).
So only “pure” data and no “physically” (inspector visible) GameObject.
Case Two: If I want to drop “physically” an item in my scene, lets say a piece of wood that can be picked up from somebody else for example, I instantiate a new Item GameObject and assign the corresponding values from Item (sprite, name, …) and int amount according to the inventory to the created Item GameObject. This GameObject Item can be transformed as child to anything you want.
On the side: To increase the readability of your post you can create an empty line using: