How to convert object types?
Hello, I create some items, maked like scriptable objects. For example:
Item : ScriptableObject
{
public int itemID;
public string itemName;
public void Use()
{
// Nothing to do here
}
}
Resources : Item
{
public weight;
// 2 items here
// Wood
// Iron plate
}
Product : Item
{
public List<Item> recipe = new List<Item>
// 1 item here
// Iron shovel, in list I add two assets in inspector: wood and iron plate
public override void Use()
{
if(inventory.CheckItem(inventory.GetItemById(itemID)))
// Do something
}
}
So I want to check if item exist in inventory then destroy them and add shovel, but can't do this because of object type. As you can see if player click on iron shovel, script can't check item, because inventory script hold items as Item, but resources has another type Resource, so that's the problem. What the best way to avoid such things?
As I thought first way is delete subclasses and create all items in one class, just use enum for declare itemType, but a lot of null variables will be on each item.
Second way is use enum and check itemType in inventory script, then return needed item type, but a lot of if statements will be in code.
Maybe someone advice me more fast and clear way?