• 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
1
Question by eggorko · Aug 17, 2017 at 08:26 PM · serializationscriptableobjectdatabasecustom-editorcustom class

Can’t serialize custom class variable in ScriptableObject.Or add instance of a class to a list with overridden method in custom editor.

I’m making simple inventory system. I got a class for an Item

 [System.Serializable]
 public class Item 
 {
     public string itemName;
     public int itemId;
     //some more variables
     public Effect itemEffect;
     //some more code
 public void Use(Entity x)
     {
         itemEffect.UseEffect(x);
     }
 }

All the items are stored in the list inside ScriptableObject with a custom editor. The best way of implementing item effect I came up with is t$$anonymous$$s. Every item has method “Use” w$$anonymous$$ch is calling “UseEffect” method of itemEffect variable w$$anonymous$$ch is an instance of Effect class T$$anonymous$$s is the Effect class.

 [Serializable]
 public class Effect 
 {
     public string effectName;
     public Effect ()
     {
         t$$anonymous$$s.effectName = "BaseEffect";
     }
 
     public virtual void UseEffect (SomeParameter x)
     {
         Debug.Log("T$$anonymous$$s  is base effect"+ x.Somet$$anonymous$$ng.ToString());
     }
 }

T$$anonymous$$s is my Test Effect derived from Effect class

 public class TestEffect: Effect
  {
     public TestEffect()
     {
         t$$anonymous$$s.effectName = "TestEffect";
     }
     
     public override void UseEffect(SomeParameter x) 
     {
         Debug.Log("T$$anonymous$$s is TEST EFFECT");
         x.somet$$anonymous$$ng = x.somet$$anonymous$$ng+1;
         Debug.Log(x.somet$$anonymous$$ng.ToStirng());    
     }
  }

T$$anonymous$$s is how i assign TestEffect object to Item variable i custom editor.

  if (GUI.Button(//someRect,"Apply test effect to test item"))
         {
           //Item_Database_Asset is the ScriptableObject  with Items_Database_List of all items                           
           Item_Database_Asset.Items_Database_List[1].itemEffect = new TestEffect();;
           EditorUtility.SetDirty(Item_Database_Asset);
           
         }

So the problem is after i click "Apply test effect to test item" when i press play and using t$$anonymous$$s item it’s calling UseEffect of BaseEffect. Although in inspector i see that item[1] itemEffect.effectName is TestEffect. So is basically working but it’s still calling BaseEffect. But if i click "Apply test effect to test item" on runtime in play mode it works fine. It's a bit long and convoluted.I hope it's readable. Thank you.

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

Answer by Bunny83 · Aug 18, 2017 at 12:46 AM

Unity has no polymorp$$anonymous$$sm support for custom serialized classes. Read the documentation about serialization. Especially the part:

When might the serializer behave unexpectedly?

Custom serialized classes are mainly meant to be data containers. The serializer serializes custom classes based on the variable type, not the actual type.

Comment
Add comment · Show 3 · 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 eggorko · Aug 18, 2017 at 05:39 AM 0
Share

Thank you, you are right

No support for polymorphism

I didn't know that. Well, it's so stupid i googled it and found a request to add support of this back in 2012. Is there any workaround or better yet can propose another way to assign method from one script to object from another(well scriptable-object) in the editor.

My first approach was a save list of delegates/Actions, turns out delegates are not serializable (duh). Then i tried this. I'm out of ideas.

avatar image Bunny83 eggorko · Aug 18, 2017 at 08:13 PM 0
Share

Well, there are alternatives. However it depends on what's your goal.

First of all if you just want to serialize method references you can use a UnityEvent. It's basically a serializable delegate class, however with some differences. You can only serialize delegates to methods that belong to a class that is derived from UnityEngine.Object as those are the only class references that can be serialized. The only baseclasses you can actually derive your classes from are MonoBehaviour and ScriptableObject.

If you actually need polymorphism / inheritance and you want to serialize the references you have to derive your base class either from MonoBehaviour or ScriptableObject. Since those can be stored as assets Unity can serialize references to them. While MonoBehaviours are components which need to be attached to a GameObject (in the scene or to a prefab asset), ScriptableObjects need to be serialized as seperate assets.

avatar image eggorko Bunny83 · Aug 19, 2017 at 10:19 AM 0
Share

Thank you, i will try both things just to see which is more appropriate for my thing.

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

74 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

Related Questions

Referencing a class defined within a ScriptableObject 0 Answers

Scriptable Object's Data Gets Lost After Re-opening Unity!!! 1 Answer

Making an RPG Maker VX esque database using ScriptableObject, with custom editor 1 Answer

How do I pass serialized ScriptableObjects between Unity projects? 2 Answers

.asset file containing ScriptableObject is empty on openingUnity 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