Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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
1
Question by Kirbyrawr · Sep 22, 2016 at 11:01 PM · serializationinheritanceclasses

Derived Class Serialization

Hi there, first of all i searched all google trying to found a easy fix for this with misleading results.

QUESTION

I have this:

 [System.Serializable]
 public class ItemModuleData {
 
 }

 public class ItemModule {
   public ItemModuleData data;
 }
 
 [System.Serializable]
 public class EquipableData : ItemModuleData {
 
 }
 
 public class EquipableModule : ItemModule {
  public new EquipableData data;
 }


I want to serialize both classes (ItemModuleData and EquipableData), for do things like this:

 ItemModule a;
 a.data = anyModuleData;

PROBLEM

Unity can't serialize inherit class, and it will throw an error like this:

 The same field name is serialized multiple times in the class or its parent class. This is not supported: Base(MonoBehaviour) data

I can't also use ScriptableObject since i don't want to have a lot of .assets in my project and it makes no sense in this case at least for me.

Thanks in advanced.

Comment
Add comment · Show 3
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 Cherno · Sep 23, 2016 at 12:18 AM 0
Share

Have you tried putting the inheriting classes in their own script files?

avatar image Kirbyrawr Cherno · Sep 23, 2016 at 05:33 AM 0
Share

Yeah no luck so far. Actually i have them separated.

avatar image Kirbyrawr · Sep 23, 2016 at 02:26 PM 0
Share

Bumping the answer for better visibility.

3 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Bunny83 · Sep 23, 2016 at 02:51 PM

If you do a quick search about that topic you'll find tons of other questions like those:

  • properly serialize array of derived classes

  • does unity serialization support inheritence

  • serializedproperty and abstract custom classes

  • list of different classes that all inherit from one class

  • serialization in unity

I've stopped counting how many times i've answered that question ^^.

In short: Unity does not support inheritance for custom serializable classes. Those classes are not serialized on their own but simply as "child data" of the containing MonoBehaviour / ScriptableObject. The type of an instance is not saved at all. Therefore if you store a derived class in a base class variable it will turn into a base class instance after deserialization.

Likewise since the custom classes aren't stored as seperate instances you can't have cross references. If two MonoBehaviour classes reference the same custom serializable class instance, after deserialization each will have their own instance. Again: Custom serializable classes are just a neat way to manage serialized fields in a MonoBehaviour.

Of course that's only true for Unity's serialization system.

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 Kirbyrawr · Sep 23, 2016 at 05:28 PM 0
Share

Thanks for the answer

avatar image Ruzihm · Mar 10, 2021 at 06:56 AM 0
Share

While correct and very useful when originally written and for some time after, this is no longer the case. See my answer below!

avatar image
9

Answer by Ruzihm · Mar 10, 2021 at 06:55 AM

The accepted above answer is out of date with the introduction of the SerializeReference decorator. Adding this decorator to the above code will now produce the desired outcome:

 [SerializeReference] ItemModule a;

and

 [SerializeReference] public ItemModuleData data;

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 Josiah_Kunz · Mar 30, 2021 at 03:31 AM 0
Share

@Ruzihm wow, thank you so much! You've saved me a lot of time.

avatar image kornel-l-varga · Apr 22, 2021 at 11:20 AM 0
Share

I feel that I have to speak up right here. First of all, thank you very much @Ruzihm !

I have been searching for this solution for almost a day straight and I only got condescending answers as the accepted one above or C# documentation similarly bot other way condescending saying that not allowing the serialization is to prevent bad design. I personally think that the "user"/programmer should be allowed the freedom of mistake and to learn form it. And perhaps in some cases the solution might not even be bad design but the only solution, and who am I to decide without deep knowledge of the ongoing project.

The other problem I see here is with the Unity community (usually amazing). I mean shouldn't we trace back onto forums where we did not find THE answer and help others? Especially when it comes to an issue that emerges so many times... anyway that's what I'm gonna do, dropping an FYI wherever I didn't find the answer.

avatar image
1

Answer by flaviusxvii · Sep 23, 2016 at 03:06 PM

@Bunny83 is correct as usual, but I recently discovered https://github.com/jacobdufault/fullserializer and it is amazing so far. It handles inheritance for you.

Comment
Add comment · Show 1 · 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 Kirbyrawr · Sep 23, 2016 at 05:29 PM 0
Share

Thanks for the answer, in my honest opinion json vs a seralized class it's really slow however maybe i'm wrong!

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

59 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

Related Questions

Saving class data without having lots of references? 1 Answer

Serializing invetory items data and InvalidOperationException exception 0 Answers

Inheriting from classes from other files (Javascript) 1 Answer

How to make an array inherit from a class? 2 Answers

Inheritance and Serialization 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