I’ve attempted the other fixes I’ve seen on Unity answers and Stack Exchange, and I’ve added breakpoints everywhere within my code and still can’t find the thing actually causing the problem. The error is on the first line attempting to add to the Items list.
This made me think that I had to instantiate the list, which is what I attempted to do in my solution, which also threw an exception.
What I started having the problem with:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ItemList : MonoBehaviour {
public static Sprite[] sprites;
// Use this for initialization
void Start () {
//load item sprites
sprites = Resources.LoadAll<Sprite>("female_robe");
}
// Update is called once per frame
void Update () {
//Debug.Log(sprites.Length);
}
public class Item
{
public string Name { get; set; }
public int Rarity { get; set; }
public int Id { get; set; }
public Sprite sprite { get; set; }
public string Type { get; set; }
}
public static List<Item> Items = new List<Item>();
public static void generateItemsChest()
{
Items.Add(new Item() { Name = "Note", Id = 1, sprite = sprites[0], Type = "Quest" });
Items.Add(new Item() { Name = "Sword", Id = 2, sprite = sprites[1], Type = "Weapon" });
Items.Add(new Item() { Name = "Shield", Id = 3, sprite = sprites[2], Type = "Armor" });
Items.Add(new Item() { Name = "Spear", Id = 4, sprite = sprites[3], Type = "Weapon" });
Items.Add(new Item() { Name = "PoleArm", Id = 5, sprite = sprites[4], Type = "Weapon" });
}
}
After looking around I learned that I needed to instantiate the list(?) so I tried this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ItemList : MonoBehaviour {
public static Sprite[] sprites;
//public static List<Item> Items = new List<Item>();
// Use this for initialization
void Start () {
//load item sprites
sprites = Resources.LoadAll<Sprite>("female_robe");
}
// Update is called once per frame
void Update () {
//Debug.Log(sprites.Length);
}
public class Item
{
public static List<Item> Items;
public string Name { get; set; }
public int Rarity { get; set; }
public int Id { get; set; }
public Sprite sprite { get; set; }
public string Type { get; set; }
public Item()
{
Items = new List<Item>();
}
}
public static void generateItemsChest()
{
Debug.Log(Item.Items.Count);
Item.Items.Add(new Item() { Name = "Note", Id = 1, sprite = sprites[0], Type = "Quest" });
Item.Items.Add(new Item() { Name = "Sword", Id = 2, sprite = sprites[1], Type = "Weapon" });
Item.Items.Add(new Item() { Name = "Shield", Id = 3, sprite = sprites[2], Type = "Armor" });
Item.Items.Add(new Item() { Name = "Spear", Id = 4, sprite = sprites[3], Type = "Weapon" });
Item.Items.Add(new Item() { Name = "PoleArm", Id = 5, sprite = sprites[4], Type = "Weapon" });
}
}