I’ve been modifying the Coding with Unity Scriptable Object Inventory tutorial project from Youtube. In the tutorial it uses a 3rd person character and a collision to add an item to the inventory(Also a scriptable object). I want to use a loot window style where the items you can pick up are on a canvass/panel. Currently it is giving me a null reference error when I try to add an item as if it cannot find the information from the scriptable object. So I’m taking a different tactic. I want to be able to preload the prefab item with the information. What is the best way to go about this?
This script is going on the prefab.
public class ArmorItem : MonoBehaviour
{
public Armor ArmorPiece;
public Item item;
public Armor Id;
public Armor Icon;
public Armor description;
public Armor weight;
public Armor value;
public Armor Prefab;
public Armor MinProtection;
public Armor MaxProtection;
public Armor ArmorPoints;
public Armor.armorslot armorSlot;
public Armor.armorclass armorClass;
public void GetArmor()
{
item.Id = ArmorPiece.Id;
item.Icon = ArmorPiece.Icon;
item.type = ArmorPiece.type;
item.description = BronzeCuriss.description;
item.value = BronzeCuriss.value;
item.weight = BronzeCuriss.weight;
item.Min = BronzeCuriss.MinProtection;
item.Max = BronzeCuriss.MaxProtection;
armorSlot = BronzeCuriss.ArmorSlot;
AP = BronzeCuriss.ArmorPoints;
armorSlot = BronzeCuriss.ArmorSlot;
}
}
Here is the Armor Scriptable Object:
public class Armor : ItemObject
{
//ID type(A armor, w weapon, c consumable, g general), class( light, medium, heavy), slot(head, torso, belt, feet, shield), number
public int MinProtection;
public int MaxProtection;
public int ArmorPoints;
public enum armorslot { Head, Torso, Belt, Feet, Shield }
public armorslot ArmorSlot;
public enum armorclass { Light, Medium, Heavy }
public armorclass ArmorClass;
private void Awake()
{
type = ItemType.Armor;
}
}
and Finally, the Root script, Item Object
public enum ItemType
{
Armor,
Consumable,
Clothing,
Beverage,
Food,
Pack,
Herb,
Tools,
Light,
QuestItem,
Weapon
}
public abstract class ItemObject : ScriptableObject
{
public int Id;
public Sprite Icon;
public ItemType type;
public string description;
public int value;
public int weight;
public GameObject prefab;
public Item CreateItem()
{
Item newItem = new Item(this);
return newItem;
}
}
[System.Serializable]
public class Item
{
public string Name;
public int Id;
public Item(ItemObject item)
{
Name = item.name;
Id = item.Id;
}
}
I’m still new to scriptable objects and would appreciate some help. Thanks!