- Home /
Wrong serialization when using ScriptableObject + [System.Serializable] + inheritance
Hey guys !
I'm having a really weird (and annoying issue) at the moment..
Basically let's say I have something like this.. (sorry I don't like copy/pasting code but it should be easier to explain with this simple test code..)
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization;
[System.Serializable]
public abstract class BaseClass : ScriptableObject
{
public enum eType
{
Type1 = 0,
Type2,
// etc..
}
public eType m_Type = eType.Type1;
public bool m_Enabled = true;
public virtual void Awake()
{
Debug.Log(m_Type + " Enabled = " + m_Enabled);
}
}
[System.Serializable]
public class Type1Class : BaseClass
{
public GameObject m_PrefabToInstantiate = null; // 3
public enum eSecondaryType
{
AAA = 0,
BBB,
CCC,
}
[SerializeField] // 1
public eSecondaryType m_SecondaryType = eSecondaryType.AAA;
[SerializeField] // 2
public bool m_MySecondaryBool = true;
public Transform m_Transform; // 4
public Type1Class()
{
m_Type = BaseClass.eType.Type1;
}
public override void Awake()
{
base.Awake();
Debug.Log("From Type1Class: SecondaryType = " + m_SecondaryType + " MySecondaryBool = " + m_MySecondaryBool);
}
}
In the editor when I start my game, everything's working fine..
The Awake Functions both display what I'm expecting, so the serialisation is working here.. meaning Type and SecondaryType both display what I have in the UI (same for the 2 test booleans)
But strangely on iOS, the Awake function of Type1Class always return the default values.. where it's all fine for the BaseClass It's like if the serialisation was only done for the base class only..
Note that removing serialisation fields (markers 1 and 2) doesn't change anything at all and still can't get correct serialised values for m_SecondaryType and m_SecondaryBool
But it's not totally not working as the GameObject and Transform members are both correct... (markers 3 & 4) I guess I must be missing something obvious here related to basic type..
I will tomorrow try with other type like string or int...
Don't tell me to use MonoBehavior instead of ScriptableObject for my BaseClass it's just not possible for the design of the system I've implemented..
Anyway I have seriously no idea what is going on... so please if you have any idea don't hesitate as I'm really stuck on that right now... and it's pretty frustrating !
I'm pretty sure serialization doesn't support inheritance in Unity.
Answer by angrypenguin · Feb 26, 2014 at 08:57 AM
As a heads up for anyone who may stumble upon this in future, abstract classes may be a potential issue.
I'm pretty sure I had sub-classes of an abstract parent serializing and deserializing earlier today without an issue. However, on a separate unrelated project (so other things could potentially be causing issues - testing has been very limited) (de)serializing to an asset outside of a scene was failing, and making the parent class non-abstract made it work.
Answer by mulova · Dec 17, 2011 at 04:58 PM
he then went on to suggest "If you must use inheritance you have to inherit from ScriptableObject", which is what OP did, isn't it?