Search Unity

ScriptableObject serialization in prefabs

Discussion in 'Scripting' started by VesuvianPrime, Oct 26, 2014.

  1. VesuvianPrime

    VesuvianPrime

    Joined:
    Feb 26, 2013
    Posts:
    135
    Hi guys

    I've hit a very worrying problem with ScriptableObject, and I'm not sure what the correct way to proceed is.


    For a little backstory, there is a known bug with ScriptableObject and the use of SerializedObject.FindPropertyRelative: it always returns null.

    http://answers.unity3d.com/questions/543010/odd-behavior-of-findpropertyrelative.html

    I have made and submitted bug reports, but I haven't heard anything in months.


    I have an object hierarchy that looks like this:

    HydraParticleEmitter (MonoBehaviour)
    ---> EmissionShape (ScriptableObject)
    -------> Child data (ints, floats, you name it)


    In a perfect world I would be able to use FindPropertyRelative when drawing my custom inspector for the grandchild properties. Because of the aforementioned bug this is impossible.

    My workaround for this has been to do the following:

    Code (CSharp):
    1. private void DrawShapeContent()
    2. {
    3.     // Note I am having to create a new SerializedObject to get access to the children
    4.     SerializedObject emissionShape = new SerializedObject(m_EmissionShapeProp.objectReferenceValue);
    5.     EmissionShapeInspector.DrawInspector(emissionShape);
    6. }

    This works great for instances (despite not working with multi-object-editing):




    However, it fails miserably when drawing the inspector for a prefab:



    The error comes from the line:

    Code (CSharp):
    1. SerializedObject emissionShape = new SerializedObject(m_EmissionShapeProp.objectReferenceValue);

    It turns out that m_EmissionShapeProp.objectReferenceValue is returning null when drawing the inspector for a prefab.


    My questions are:
    • SerializedProperty and ScriptableObject have been broken for a long time, and I'm really not finding many google results for these problems. This suggests that my workflow is way off the beaten path. Can anyone advise on how I can correct my object hierarchies so they will work correctly with Unity's serialization?
    • Why does SerializedProperty.objectReferenceValue return null when the parent is a prefab?

    Thank you for any advice you may have. I sympathise with anyone who has battled with Unity Serialization in the past.

    Ves
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,442
    VesuvianPrime likes this.
  3. VesuvianPrime

    VesuvianPrime

    Joined:
    Feb 26, 2013
    Posts:
    135
    Thanks mgear, I've replied to the thread.