- Home /
Inheritance problem when saving to prefab
I'm having some problem with inheritance, when saving it to prefabs.
I have two classes:
class A { public virtual void Call() { Debug.Log("Hello A"); } }
class B : A { public override void Call() { Debug.Log("Hello B"); } }
I then create an empty prefab, create a game object, add a simple script to hold my object of type B, and lastly replace the prefab with my game object (all done with a script).
The simple script will in Awake do this: someObj.Call();
If I do this on runtime. That is create empty prefab, and so on. Then it will output: "Hello B".
If I do this on editortime, and then add the created prefab to the scene. Then it will output: "Hello A".
So my question is: How do I get around this problem, without creating several lists for each type?
Eh, this is something new for me. I gotta try reproduce it when I get back later tonight. oO
Well, its also new for me :-) Still looking for a solution.
Answer by Ejlersen · Apr 11, 2011 at 06:05 PM
Okay, so I found this:
Unity's serialization assumes that the type of an array does not change from element to element. This is one of the basic assumption in the system and allows us to very fast serialization, while at the same time allowing full backwards compatibility.
This makes it impossible to use inheritance in a serialized embedded class. If you must use inheritance you have to inherit from ScriptableObject.
Its from 08-21-2008, but it seems its still valid.
@Anders Ejlersen oh my god! .......... i was waiting for this answer for so long!!!..... finally ...thank you sir
Your answer
Follow this Question
Related Questions
adapting replace prefab example 1 Answer
Custom inheritance classes and custom inspectors 2 Answers
Change angle of camera in prefab preview 4 Answers
Apply only a single value to prefab 1 Answer