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
3
Question by aosome23 · Apr 25, 2015 at 10:56 PM · polymorphism

Scriptable Object and Polymorphism

So I have a scriptable object which has a list of BaseClass. However, there are other classes that I wrote which inherits from the BaseClass. So I decided to use Polymorphism and shove everything in that list. For a second, I thought everything was going fine... BUT, after I restarted unity, all the objects that were inherited from the base class turned into base classes. (I cant type cast it, and gives me an error "cannot cast from source type to destination type")

I suspect this is a problem from serialization or a problem on how I import my objects.. This is how I import it:

 itemListData = (ItemDatabase)AssetDatabase.LoadAssetAtPath(currentItemListLocation, typeof(ItemDatabase));

And this is my database class

 public class ItemDatabase : ScriptableObject {
     public List<BaseClass> itemList = new List<BaseClass>();
     
 }

Comment
Add comment · Show 2
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 Baste · Apr 25, 2015 at 11:43 PM 0
Share

I believe that when you do LoadAssetAtPath(path, typeof(ItemDatabase)), you're telling Unity the exact type that it should load the object as.

There's a problem with Unity not serializing polymorphic classes very well (ie. if you have a list of Animals, and put Cats and Dogs in there, they'll come out of serialization as just Animals), but using ScriptableObjects is one of the ways you get around that problem. I use serialized scriptable objects of derived types all the time, but those are linked directly on objects, not loaded from resources, and in that case everything works fine.

Try to load with the exact type ins$$anonymous$$d of the base type, and see if that's a possible solution.

avatar image aosome23 · Apr 26, 2015 at 01:20 AM 0
Share

Thank you for the answer.

I am using scriptable object to store a list of Item. Item is a class and there are a lot of classes inherited to Item(Some of them are inherited to these subclasses).

Also it would be hard to have lists for every type since I have at least 10 different classes inheriting from the base class. Are there any solutions to serialization and polymorphism? >.<

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Skrimel · Jun 05, 2020 at 08:20 PM

Late answer, but in modern versions of Unity you can find SerializeReferenceAttribute (https://docs.unity3d.com/ScriptReference/SerializeReference.html), which will help to serialize entities with support of their polymorphism. Also look at serialization rules and read all restrictions patiently. There are some undocumented features/bugs in serialization system, but in the most cases you won't even feel them.

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 Skrimel · Jun 21, 2020 at 09:07 PM 0
Share

Also, one weird "bug": Unity support SerializeReferenceAttribute for use with fields of type, that is a subclass of a UnityEngine.Object, but it leads to memory "leaks" - everyrhing works fine, except it takes much more time for hot reloads, that are very often in work process in Unity.

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

21 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

Related Questions

An OS design issue: File types associated with their appropriate programs 1 Answer

passing paramters to a subclass? c# 2 Answers

Character Class Hierarchy with soldier classes and friendly/enemy character 1 Answer

Draw Inspector For Most Derived Type Of Collection Elements 3 Answers

Polymorphic weapon system 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