0

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

jason
  • 1,132
  • 14
  • 32
  • Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Ken White Oct 02 '20 at 20:06
  • Add some log statements into your code and see which field or property is null. – Ben Rubin Oct 02 '20 at 20:22
  • Where is the best place to attach the script? – jason Oct 02 '20 at 20:38
  • 1
    Is the line: if (instance = null) a typo just here, if not fix that in your code to: if (instance == null). – Nikaas Oct 03 '20 at 06:51
  • @Nikaas Thanks a ton! I didn't catch that. I guess I'm having one of those weeks! – jason Oct 03 '20 at 10:41

0 Answers0