Search Feedback


Serialization of Polymorphic Data Types




You should be able to Serialize polymorphic data types. A quick example:

[Serializable] class Base { public int myInt; }
[Serializable] class Derived : Base { public string myString; }

class MyBehaviour : MonoBehaviour
public Base _myBase = new Derived(); // Error... This will NOT work, only myInt will be saved.

ScriptableObject does not solve this issue because they cannot be saved into prefabs. Full post on this subject here:

Please consider this when rewriting the serialization to support nested prefabs!

Comments (44)

  1. 67cafeccd2e93148be7832e5ba5df51d?d=mm


    Nov 15, 2017 17:52

    Not supporting this violates the contract of SerializableAttribute just like not supporting circular references in object graphs.

    If you aren't going to support Serializable, you should just create a different interface to make that explicit.

  2. 67195a1a7dfa9eef8563f70a76c92a58?d=mm


    Jul 31, 2017 10:49

    why not?

  3. Bd8b27c07a270dc81f2f370d7f2bfab9?d=mm


    Jul 29, 2017 10:23

    +8 votes from me.

  4. 78ed265f0b33e2208570927bc7b5dcb4?d=mm


    Jun 27, 2017 20:54

    This has been posted as a request since 2002. The community is begging for the very much needed overhaul to the serialization system. This suggestion alone has almost 2500 votes; nevermind the countless others on the same subject of basic serialization stuff unity should be able to do. Yet...Nothing... Even as a beginning developer, just finishing my first commercial-release game, this is making me seriously consider switching to Unreal4... If I'm going to spend the time to learn this, might as well learn the right stuff, in an engine that actually has a future... And respect for the community's needs regarding the engine's features; neither of which I'm seeing here in Unity. =(

  5. 21448c4be0ee84cd5ad0558811c9be81?d=mm


    May 28, 2017 19:16

    If this doesn't get implemeted soon, I may switch to unreal, or godot, or jıst any pther engine that has a sane way of scripting.
    +5 votes from me.

  6. Cfc92d15d6e1d20ea0afb32f27ed25d9?d=mm


    May 25, 2017 19:01

    I want 100 votes more to put here

  7. C1add2ff230213b25de9d56e8f77b97b?d=mm


    Jan 23, 2017 15:52

    I'm out of votes but I support this!

  8. 3cbf274fa13971491fb6eea6c88db922?d=mm


    Jan 19, 2017 14:31

    You could try this:!/content/79135
    It enables you to display and serialize polymorphic types, dictionaries, properties, generic types, HashSets and delegates.

  9. 38094335cb7c5bc3a9fa46926958f326?d=mm


    Jan 03, 2017 11:01

    Fix this already..

  10. D908420bf326ab17239235b62d4e5103?d=mm


    Dec 19, 2016 18:21

    Good job unity, version 5.5 has been released and nothing has changed !!!
    You force developers to write bad unmaintainable code.

    Unity Version 9.99 (Galaxy Edition) released after 1.000 years:
    a) Unity editor is 1 millisecond faster.
    b) 0.0001% better graphics. Despite 99999 assets doing the same thing = Redundancy.
    c) Serialization system not fixed, please use regular assembly for "object construction".

    Full inspector does it, why is unity team lazy to support scripting. Forcing us to write code in assembly without interfaces.

    So i have an interface with 10 implementations ? Regular c#: 1 line of code to instantiate.
    Unity Way? 11 classes of 10 lines of code each = 110 lines of code. Really 11000% more code?

    Conclusion: Unity is a piece of crap, that tries to vendor lock you to its crap.
    1) Monobehaviour? doesn't serialize interfaces.

    2) ScriptableObject? Thank you for forcing us to :
    a) create 1 asset per instance. Leading to pollution of lots small files.
    b) waste 5 min productivity loss, to "switch to project", "create new scriptable object", "lose inspector focus", "set values", "return back to scene", "return inspector focus", "remember where the hell you left it off".
    c) no way to store the scriptableObject in the "scene" like monobehaviour or in a "prefab".
    d) pollute the editor namespace with ScriptableObject types, good luck searching in a list of 1000 scriptableObject types, just to instantiate one. Those Menus LOVE YOU. I am wondering how long will it take you to reach the 999th one.

    3) Subassets ? Not supported. No editor gui, no "Drag and drop", no "create". Nothing.

    So to write "Good code" you are forced to :
    a) Never use unity gui or editor.
    b) Inline all objects in code. If you want to make a slight modification, you have to wait 1,5min compile time. Unity Loves you.
    c) assembly loves you.

Your opinion counts

Help us make things better. Share your great idea for improving Unity or vote for other people’s.

Log in to post a new idea








AI & Navigation






Asset Store






Cloud Build




Docs & Tutorials






Game Performance Reporting


















Profiling & Optimization