• 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 adriandevera · Aug 13, 2018 at 01:14 AM · instantiateinstancesaveloadscriptable object

Scriptable Object storing Instantiated Objects Show Mismatch Types

I am trying to use scriptable objects as a precompiled database of cards which are also scriptable objects. The below works except the scriptable database object shows a type mismatch. I have checked here for solutions and cannot seem to determine the issue. The scriptable carddatabbase contains the card definitions like so:

 [CreateAssetMenu(fileName = "New Card Database", menuName = "Game/Card Database")]
 public class CardDatabase : ScriptableObject
 {
     // definition of all cards in the game by name
     public Card heavySlash;
     public Card herbalBandage;
 }

Here is an extended card database for the player to load their current game from. This will call the saving method which will be mentioned further down the post:

 [CreateAssetMenu(fileName = "New Card Database", menuName = "Game/Player Card Database")]
 public class PlayerCardDatabase : CardDatabase
 {
     public List<Card> cards;
 
     public void Clear()
     {
         cards = new List<Card>();
     }
 
     public void Save(List<Card> deck)
     {
 
         foreach (Card card in deck)
         {
             Card newInstance = new Card();
             newInstance = card;
             cards.Add(newInstance);
 
             // Note: The below works obviously but the memory is shared
             // I want to have each card object independent so I can modify them during runtime
             // cards = deck; 
         }
     }
 }

The cards store standard info such as name and attack stats and other attributes.

 [CreateAssetMenu(fileName = "New Card", menuName = "Game/Card")]
 public class Card : ScriptableObject {
     //name of card
     public new string name;
     public CardType type;
     public Texture texture;
     public int damage;
     public int manaCost;
     public bool destroyEquipment;
     public int heal;
 }

During runtime I generate the player cards saving it to the scene list. These are typically loaded but in this instance on a new game well call this function instead to instantiate a deck, add copies of our scrip table objects via Instantiate and then save it to load for later:

          void CreateCards(bool firstTime = false)
         {
             if (firstTime)
             {
                 //flush out our cards, will be moved to when new game button is created
                 playerCardDatabase.Clear();
 
                 Card heavySlash2 = Instantiate(cardDatabase.heavySlash);
                 Card herbalBandage2 = Instantiate(cardDatabase.herbalBandage);
 
                 for (int i = 0; i < 7; i++)
                 {
                     deck.Add(heavySlash2);
                 }
 
                 deck.Add(herbalBandage2);
                 playerCardDatabase.Save(deck);
             }
         }

Unfortunately when the playerCardDatabase.Save(deck); is called the inspector shows mismatype of cards.

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 adriandevera · Aug 13, 2018 at 04:13 AM

I redid most of the code but in short I had a Scriptable object ScriptableCard, a regular class Card and a ScriptableObject CardDatabase.

I simply make a copy to inherit the information from ScriptableCard using the below below:

     public Card Copy()
     {
         Card card = new Card();

         card.name = this.name;
         card.type = this.type;
         card.sprite = this.sprite;
         card.texture = this.texture;
         card.damage = this.damage;
         card.manaCost = this.manaCost;
         card.destroyEquipment = this.destroyEquipment;
         card.heal = this.heal;

         return card;
     }

During runtime I would call it like so:

                 Card heavySlash = cardDatabase.heavySlash.Copy();
                 deck.Add(heavySlash);

Hopefully this helps others in the future..

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

108 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Updating a variable on a script in an instanced object 1 Answer

prefab spawning with variable delay 0 Answers

NullReference on trying to instantiate a prefab which has been set using scriptable object,Particle giving null reference but works in scene view 0 Answers

Duplication(instance) re-create deleted children 0 Answers

Instantiated GameObject being deleted when destroying original 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