Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Scriptable object list of scriptable opjects

Discussion in 'Scripting' started by KaHeer, May 18, 2020.

  1. KaHeer

    KaHeer

    Joined:
    Nov 19, 2018
    Posts:
    5
    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):
    1. using System.Collections.Generic;
    2. using UnityEngine;


    3. [CreateAssetMenu(fileName ="Database", menuName ="Assets/Database")]

    4. public class CardDatabase : ScriptableObject
    5. {
    6. public List<CardData> allCards;
    7. }
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class Test : MonoBehaviour
    5. {
    6. public CardDatabase database;

    7. //reference to database list
    8. List<CardDatabase> cards = new List<CardDatabase>();

    9. public IEnumerator Start()
    10. {
    11. database = GetComponent<CardDatabase>();
    12. yield return new WaitForEndOfFrame();
    13. foreach(CardData c in database.allCards)
    14. {
    15. Debug.Log(c.name);
    16. }
    17. }
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    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?
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    John_Leorid and lordofduct like this.
  4. KaHeer

    KaHeer

    Joined:
    Nov 19, 2018
    Posts:
    5
    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+<Start>d__5.MoveNext () (at Assets/Scripts/Test.cs:17)
    UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) (at <480508088aee40cab70818ff164a29d5>:0)
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    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?
     
  6. KaHeer

    KaHeer

    Joined:
    Nov 19, 2018
    Posts:
    5
    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.
     
  7. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    You already have a public field for your card database. Just drag the ScriptableObject into it in the inspector.
     
    KaHeer likes this.
  8. KaHeer

    KaHeer

    Joined:
    Nov 19, 2018
    Posts:
    5
    Yup, I am an idiot, knew it was something simple like that.
    Thanks for the help guys.