- Home /
 
Why is my SerializedProperty.objectReferenceValue Not Accepting my Object?
In the following code...
     [CustomEditor(typeof(Unit))]
     public class UnitEditor : Editor
     {
         private Editor behaviourEditor;
 
         public override void OnInspectorGUI()
         {
             base.serializedObject.Update();
 
             SerializedProperty behaviourProperty = base.serializedObject.FindProperty("behaviour");
             UnitBehaviour behaviour = behaviourProperty.objectReferenceValue as UnitBehaviour;
             MonoScript behaviourScript = behaviour != null ? MonoScript.FromScriptableObject(behaviour) : null;
 
             EditorGUI.BeginChangeCheck();
             {
                 behaviourScript = EditorGUILayout.ObjectField("Behaviour", behaviourScript, typeof(MonoScript)) as MonoScript;
             }
             if (EditorGUI.EndChangeCheck())
             {
                 if (behaviour != null)
                 {
                     DestroyImmediate(behaviour);
                     behaviour = null;
                 }
                 if (behaviourScript != null && behaviourScript.GetClass().IsSubclassOf(typeof(UnitBehaviour)))
                 {
                     behaviour = ScriptableObject.CreateInstance(behaviourScript.GetClass()) as UnitBehaviour;
                 }
             }
 
             if (behaviour != null)
             {
                 CreateCachedEditor(behaviour, null, ref this.behaviourEditor);
                 EditorGUILayout.Space();
                 this.behaviourEditor.OnInspectorGUI();
             }
 
             Debug.Log(behaviour + " : Before");// Not Null
             behaviourProperty.objectReferenceValue = behaviour;// <----- This isn't working aperently
             Debug.Log(behaviourProperty.objectReferenceValue + " : After");// Still Null
 
             base.serializedObject.ApplyModifiedProperties();
         }
     }
 
               I have a ScriptableObject of Unit and an abstract ScriptableObject of UnitBehaviour. My goal is to have only Unit stored as assets and pass in a MonoScript to declare its UnitBehaviour and embed it's inspector within the Unit's inspector. I used debug messages to figure out the issue and everything seems to be working fine except at the end when I go to apply the new UnitBehaviour to the SerializedProperty. As you can see with my comments set its value and it still shows as null. As a result applying the property wont really do much and on the next GUI update it will think the MonoScript is null and erase everything.
Your answer
 
             Follow this Question
Related Questions
Handling Undo/REdo in EditorWindow when editing DB record. 0 Answers
Script work in editor but not in build 0 Answers
Create Multiple Foldouts. 1 Answer