Hello there. I’m currently trying to set up an inventory system but am having problems. I’m planning on putting all my items in a JSON database, but know nothing of that and for the time being am just trying to get it working using a dictionary.
Anyway, I have a few items defined using a static Dictionary called items, here’s the code. (Weapon is a child of a class called Items, by the way.):
public class ItemList : MonoBehaviour {
public static Dictionary<string, Item> items;
void Start () {
items = new Dictionary<string, Item>();
items.Add( "sword", new Weapon("sword", 1, true, false, 1));
items.Add( "axe", new Weapon("axe", 2, true, false, 1));
}
}
In the script that sets up my item inventory I have the following code:
public class PlayerIO : MonoBehaviour {
List<Item> actionBar;
void Start () {
actionBar = new List<Item>();
actionBar.Add(ItemList.items["sword"]);
}
}
But I keep getting “NullReferenceException: Object reference not set to an instance of an object” on the line with “actionBar.Add(ItemList.items[“sword”]);” when I run it in Unity.
Unfortunately I don’t know enough about lists/dictionaries other than what the single video on Unity’s site taught because I can’t find any documentation. Any help is sincerely appreciated!