我在名为“Card”的数据库中设置了一个 Scriptableobject,并试图在 Scriptableobject 中创建这些项目的可搜索列表。我将 CardDatabase 脚本附加到一个名为 _CardDatabase 的空游戏对象。我不确定我是否错误地附加了它或者我在这里做错了什么。
我仍然收到以下错误:
NullReferenceException:对象引用未设置为对象 CardDatabase.GetCardByID (System.String abbr) 的实例(在 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;
}