Scriptable object list of scriptable opjects

I have been trying to make a tgc type game. I have scriptable objects with my cards data and one with a list filled with those card scriptable objects. I cant seem to use the list in my code.

Code (CSharp):

  • using System.Collections.Generic;
  • using UnityEngine;
      • [CreateAssetMenu(fileName =“Database”, menuName =“Assets/Database”)]
    • public class CardDatabase : ScriptableObject
  • {
  • public List allCards;
  • }

Code (CSharp):

  • using System.Collections;
  • using System.Collections.Generic;
  • using UnityEngine;
  • public class Test : MonoBehaviour
  • {
  • public CardDatabase database;
    • //reference to database list
  • List cards = new List();
    • public IEnumerator Start()
  • {
  • database = GetComponent();
    • yield return new WaitForEndOfFrame();
  • foreach(CardData c in database.allCards)
  • {
  • Debug.Log(c.name);
  • }
  • }

What do you mean by you can’t use it? Are you getting an error? A compiler error? An error when you run the game?

How to report problems correctly in the Unity3D forums:

http://plbm.com/?p=220

2 Likes

I am getting

ArgumentException: GetComponent requires that the requested component ‘CardDatabase’ derives from MonoBehaviour or Component or is an interface.
UnityEngine.Component.GetComponent[T] () (at <480508088aee40cab70818ff164a29d5>:0)
Test+d__5.MoveNext () (at Assets/Scripts/Test.cs:17)
UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) (at <480508088aee40cab70818ff164a29d5>:0)

You actually WROTE in your post above that your CardDatabase is a ScriptableObject and actually posted the code above. Why is the above error even remotely mysterious to you? A ScriptableObject is neither a MonoBehavior or a Component, which means you cannot use GetComponent<>()! It is really THAT SIMPLE.

What are you trying to do? Do you just intend to make a public field that you can drag a reference into?

Currently the test script is on a button. I am trying to get it to access the database and randomly pick data to attach the the cards that is being instantiated. At this point I am just using the test button as an object to call out the names of the cards in the list just to see if it is working correctly. I don’t understand how to get access to the items on the list. GetComponent () isn’t working because the cards aren’t components that I understand but what to use instead is what I’m asking.

You already have a public field for your card database. Just drag the ScriptableObject into it in the inspector.

1 Like

Yup, I am an idiot, knew it was something simple like that.
Thanks for the help guys.