NullReferenceException using ScriptableObjects in Unity

2.1k views Asked by At

I have setup a Scriptableobject in database called "Card" and am attempting to make a searchable list of those items in the scriptableobject. I attached CardDatabase script to an empty gameobject called _CardDatabase. I am not sure if I am attaching it incorrectly or what I have done wrong here.

I am still getting the following error:

NullReferenceException: Object reference not set to an instance of an object CardDatabase.GetCardByID (System.String abbr) (at Assets/Scripts/CardDatabase.cs:33)

// CardDatabase.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;

 public class CardDatabase : MonoBehaviour
    {
        public CardList cards;
        private static CardDatabase instance;
    
    
        private void Awake()
        {
            if (instance = null)
            {
                instance = this;
                DontDestroyOnLoad(gameObject);
            }
            else
            {
                Destroy(gameObject);
            }
        }
    
    
        public static Card GetCardByID(string abbr)
        {
            return instance.cards.AllCards.FirstOrDefault(i=> i.abbr == abbr);
        }
    }

// CardList.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[CreateAssetMenu(fileName = "New Cards Database",menuName = "Cards Database")]
public class CardList : ScriptableObject
{
    public List<Card> AllCards;

}

Screenshot of inspectors

0

There are 0 answers