- Home /
SerializedProperty and PropertyField NullReference
I'm having a strange problem in using EditorGUILayout.PropertyField on my EditorWindow:
There is a master object that holds a reference to lists of Items, each item containing a preview image. These 3 classes (Master, Item, Preview) are custom ScriptableObjects.
In my editor window, I construct a SerializedObject of the Master, get the item list property, and extract the item's actual value (the objectReferenceValue). This item is stored on the editor class, and later a GUI is displayed to edit the Preview's properties.
Such GUI again creates a SerializedObject out of the Item, gets its Preview property, and using FindPropertyRelative creates PropertyFields for each of the Preview's properties.
The GUI drawing function (called inside the window's OnGUI):
 private void DrawItemDetail() {
     if (_currentItem == null) {
         return;
     }
     var currentItemSerial = new SerializedObject(_currentItem);
     var previewSerial = new SerializedObject(_currentItem.PreviewImage);
 
     GUILayout.BeginVertical();
     GUILayout.Label("Preview Image Settings", EditorStyles.boldLabel);
     EditorGUI.indentLevel++;
     EditorGUILayout.PropertyField(previewSerial.FindProperty("DistanceToPivot"), new GUIContent("Distance to Pivot"));
     EditorGUILayout.PropertyField(previewSerial.FindProperty("PivotPosition"), new GUIContent("Pivot Position"));
     EditorGUILayout.PropertyField(previewSerial.FindProperty("PivotRotation"), new GUIContent("Pivot Rotation"));
     EditorGUI.indentLevel--;
     GUILayout.EndVertical();
 }
Note the 3 PropertyField calls. Whichever comes first, gives the following stack trace:
 NullReferenceException: Object reference not set to an instance of an object
 UnityEditor.PropertyHandler.OnGUILayout (UnityEditor.SerializedProperty property, UnityEngine.GUIContent label, Boolean includeChildren, UnityEngine.GUILayoutOption[] options) (at C:/buildslave/unity/build/Editor/Mono/ScriptAttributeGUI/PropertyHandler.cs:190)
 UnityEditor.EditorGUILayout.PropertyField (UnityEditor.SerializedProperty property, UnityEngine.GUIContent label, Boolean includeChildren, UnityEngine.GUILayoutOption[] options) (at C:/buildslave/unity/build/Editor/Mono/EditorGUI.cs:6837)
 UnityEditor.EditorGUILayout.PropertyField (UnityEditor.SerializedProperty property, UnityEngine.GUIContent label, UnityEngine.GUILayoutOption[] options) (at C:/buildslave/unity/build/Editor/Mono/EditorGUI.cs:6825)
 EditMode.Editor.CatalogWindow.DrawItemDetail () (at Assets/Exosphir/Scripts/EditMode/Editor/CatalogWindow.cs:220)
 EditMode.Editor.CatalogWindow.OnGUI () (at Assets/Exosphir/Scripts/EditMode/Editor/CatalogWindow.cs:89)
 System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Reflection/MonoMethod.cs:222)
It's worth noting that the 3 properties being accessed are a float, a Vector3 and a Quaternion. I have tried getting the preview property from the serialized object, then using FindPropertyRelative to get the properties, but a similar error happens. How can I fix this? 
Answer by kroltan_ · Aug 27, 2015 at 06:06 PM
Found it, the problem was that I was actually using C# properties ( {get;set;}) for the PreviewImage properties, and Unity doesn't serialize that. Changing them to fields fixed it. 
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                