Hi, I’ve been creating up a 3D TCG. I’ve set up a ScriptableObject to hold all of the cards in the game (a database), my goal is to just grab all of the card information from the ScriptableObject and instantiate each card with all the relevant information at the start of each match.
Anyway, I’ve set up the ScriptableObject here in Card.cs
using UnityEngine;
using System.Collections;
[System.Serializable]
public class Card : ScriptableObject {
public string cardName = "";
public int attack1 = 0;
public int attack2 = 0;
// there is more information in the ScriptableObject, shortened here for the sake of brevity
}
Set up the List in CardList.cs
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class CardList : MonoBehaviour {
public List<Card> cardList = new List<Card> ();
}
Then I created an Editor so I could add cards to the ScriptabelObject List: CardManager.cs
using UnityEngine;
using UnityEditor;
using System.Collections;
public class CardManager : EditorWindow {
[MenuItem("Daylight TCG/Card Manager")]
static void Init() {
CardManager window = (CardManager)EditorWindow.CreateInstance(typeof(CardManager));
window.Show();
}
string newCardName ="";
int newCardAttack1 = 0;
int newCardAttack2 = 0;
public CardList cardList;
void OnGUI()
{
cardList = EditorGUILayout.ObjectField (cardList, typeof(CardList)) as CardList;
if (cardList != null) {
newCardName = EditorGUILayout.TextField ("Troop Name: ", newCardName);
newCardAttack1 = EditorGUILayout.IntField ("Darkness Damage: ", newCardAttack1);
newCardAttack2 = EditorGUILayout.IntField ("Daylight Damage: ", newCardAttack2);
if (GUILayout.Button ("Add New Card to Database")) {
Card newCard = ScriptableObject.CreateInstance<Card> ();
newCard.cardName = newCardName;
newCard.attack1 = newCardAttack1;
newCard.attack2 = newCardAttack2;
cardList.cardList.Add (newCard);
}
}
}
}
All of this works just fine, I can add cards to the ScriptableObject List through the editor just fine. The problem comes in when I attempt to read the information from the ScriptableObject… for the life of me I can’t figure this out and can’t find a solution anywhere.
All I’m trying to do right now is just print a list of the card names I’ve added to the makeshift database. I’m sure once I get this working I can figure out the rest. Here’s the code in SetCardInfo.cs
using UnityEngine;
using System.Collections;
public class SetCardInfo : MonoBehaviour {
private CardList _currentCard;
void Start() {
_currentCard = GetComponent<CardList>();
Debug.Log ("Total Cards: " + _currentCard.cardList);
foreach (Card c in _currentCard.cardList)
Debug.Log ("Card Name: " + c.cardName);
}
}
When I run this script attached to the card in the scene I get this error:
NullReferenceException: Object reference not set to an instance of an object
SetCardInfo.Start () (at Assets/Scripts/SetCardInfo.cs:11)
Line 11 is the debug.log
Any help would be graciously appreciated, thanks!