Why can't I reference an object?

I have a class that is supposed to be a repository for all my game’s items and many other things. This repository has a static list called equipment. When creating UI I can easily use foreach on that list, but when I try to reference specific object, or reference by index, it always returns null. Even within that repo class, one line after. Does anyone know what’s going on? And how can I fix that?

Here’s the code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
namespace Assets.Scripts
{
  public class StuffRepo:MonoBehaviour
  {
    private static StuffRepo instance;
    public static List<Item> equipment;
    private void Awake()
    {
      instance = this;
      equipment = new List<Item>();
      equipment.Add(new Item("Stuff", "desc.", 1, 1));
      equipment.Add(new Item("OtherStuff", "desc2", 2, 3));
    }
  }
}

Note that static things are never serialized by Unity.

Are you perhaps looking for a ScriptableObject-based solution to your repository of equipment?

That approach is very common in Unity.

ScriptableObject usage in RPGs:

Usage as a shared common data container:

Otherwise, if you’re just getting a nullref, fix that…

How to fix a NullReferenceException error

Three steps to success:

  • Identify what is null ← any other action taken before this step is WASTED TIME
  • Identify why it is null
  • Fix that