- Home /
Serialization and Inheritance: Great-GrandChild Class appears unserialized
I have something along the lines of:
class Core : Monobehavior {
//public int value;
}
class Variation : Core {
//public int value;
}
class Specific : Variation {
public int value;
}
Alright, so, basically, I see different serialization depending on where value
is declared, but only on the app. It's fine as a scene/prefab/re-imported asset bundle. This is driving me up the wall, and I don't know what experiments to try to figure out what's going on or what might be a work-around.
A bit more detail: value
is set via the editor to something. Reliably, if it's declared in Core, it has the correct value when run on the device (android), and, reliably, it has a value of zero when declared in Specific - even if I set the default to, say, 10. Sometimes it has the correct value when declared in Variation, and I swear it depends on where I last declared it, although that's crazy - I'm pretty sure there's a pattern to the when, but I haven't been able to isolate it. It could have something to do with the code state when I create the asset bundle vs when I build-and-run, although I've tried to keep everything together.
Important Edit
The first guy who implemented our code for downloading and instantiating an asset bundle did a bunch of weird and complicated stuff I don't follow, and when I re-implemented it in the simple way recommended by Unity, everything worked.
So if you hit this question because you're having the same issue, maybe just scrap your download code and start over - however, first check to make sure all your info is in the prefab and asset bundle.
I'm leaving the question up in the hopes that I have time to dive into the wtf that is the prior code, and figure out what the difference is.
That sounds really strange... I think i've never trieds a second level inheritance with serialization, but the way Unity's serialization works it should serialize them. I have to try this myself, but it's late here and i have to sleep ;).
Is it always 0 (the default value) on android? Have you tried other playforms? Actually this kind of problem shouldn't be platform specific, but who knowa ;)
Well, also, now, serious WTF: Got rid of Core (copied it into Variation), and now it doesn't matter if specificValue is in Specific or Variation, it's zero, whereas before, if I put it in the parent classes, it worked out. WTF.
You don't really need the System.Serializable attribute since $$anonymous$$onoBehaviour derived classes are always serialized. $$anonymous$$aybe it causes this behaviour, but i don't think so.
Are you sure you don't set the values in Awake or Start or any other place?
Removed the Serializable attribute, everything is working as it was, including breaks.
I don't set the values in Awake or Start, and I got no errors when I privatized/protected the variable.