Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by JosephBlaze · Jul 22, 2014 at 09:50 PM · listscriptableobject

Reading Data from a ScriptableObject List

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!
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by JosephBlaze · Aug 08, 2014 at 06:17 AM

I eventually figured it out... I noticed people have been looking at this question. So if you are interested in the answer, it was as simple as:

 _currentCard = GameObject.Find ("CardDatabase").GetComponent<CardList>();

I wasn't targeting the correct game object.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

How to compare a string in a list with a gameobject.name from another list of gameobjects 1 Answer

Custom Inspector; adding to list not getting saved 2 Answers

Why does my List overwrite itself when using .Add? 2 Answers

Filling array with Scribtable Objects automaticly? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges