Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Ludwintor · Nov 02, 2020 at 04:09 PM · scriptableobjecttypeclass instanceimplementation

ScriptableObject problem and creating a new class instance

Hi! I ran into a problem with Scriptable Objects. I have a abstract class derived from ScriptableObject. All other cards are inherited from this class, which I assign values ​​in the inspector

     public abstract class CardObject : ScriptableObject
     {
         public Card data = new Card();
 
         public string Name { get => CardStrings.name; }
         public string RawDescription { get => CardStrings.description; }
 
 
         private CardStrings CardStrings { get => Game.Instance.languagePack.GetCardStrings(data.id); }
 
         public abstract void Play();
     }

What's the matter. There will be cards in my game, but each one will act absolutely unique, which is why I came to the conclusion that it is better for each card to make its own class, which will just inherit from CardObject

     [Serializable]
     public class Card
     {
         public delegate void CardChange();
         public event CardChange OnCardChange;
 
         [Header("Base properties")]
         public string id;
         [SerializeField] private int baseDamage = 0;
         [SerializeField] private int baseMagicNumber = 0;
 
         public int Damage { get => baseDamage; }
         public int MagicNumber { get => baseMagicNumber; }
 
         public CardObject CardObject { get => Game.Instance.cardManager.cardDatabase.GetCardById(id); }
         public Card()
         {
             id = "";
             baseDamage = 0;
             baseMagicNumber = 0;
         }
 
         public Card(string cardId) : this(Game.Instance.cardManager.cardDatabase.GetCardById(cardId)) {}
 
         public Card (CardObject card)
         {
             id = card.data.id;
             baseDamage = card.data.baseDamage;
             baseMagicNumber = card.data.baseMagicNumber;
         }

But after that I thought that it would be better to create a Card class and already inherit each new card from it, overriding only the Play() method, which will be transferred from CardObject to Card class, but I ran into a problem that I use ScriptableObject to set base values for each map, but I don't know how I can assign a specific Card derived class to a given ScriptableObject. Tell me how I can do this, or even better, if you have any ideas, tell me how you can better implement a system with completely different cards, without creating each class separately for each card.


At the moment, my Card class acts only as a class that is cloned from CardObject to create an initial card.
Also, English is my second language, so if something is not clear, then ask.

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
Best Answer

Answer by Bunny83 · Nov 04, 2020 at 10:20 AM

You just overcomplicated everything. Unity's serialization system isn't really prepared for serializing polymorphic classes besides ScriptableObjects and MonoBehaviours. Since the introduction of the SerializeReference attribute it's now possible to serialize polymorphic custom serializable classes, however you would need to handle the creation of those classes completely yourself with a custom editor.


I would highly recommend that you just stick to your ScriptableObject class and keep your data in that class. ScriptableObjects can be cloned by default by using Instantiate. In the editor you can directly use "CTRL + d" to duplicate any ScriptableObject asset. You can also create instances of ScriptableObjects at runtime by using the static CreateInstance method. ScriptableObjects, just like MonoBehaviours, do not really support constructors. However if you want to create them at runtime you can either use Instantiate to clone an existing object (asset or object in scene) or / and use an "Init" method which you call after the cloning / creating the object.


Just don't forget to add the CreateAssetMenu attribute to each of your concrete CardObject classes so you can directly create instances in the editor.

Comment
Add comment · Show 2 · 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
avatar image Ludwintor · Nov 04, 2020 at 03:09 PM 0
Share

Thanks, I will definitely try it and write if everything goes well

avatar image Ludwintor · Nov 05, 2020 at 09:21 AM 0
Share

Thank you very much, Instantiate is what I needed and everything works perfectly!

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

142 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 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

Assign a variable of type "type" on the inspector or best work around 2 Answers

ScriptableObject.CreateInstance(Type T) keep crashing 2 Answers

Null Reference Error With Scriptable Objects 1 Answer

Serialize a reference to MonoBehaviour type in ScriptableObject 2 Answers

Need help understanding scriptable objects. 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