Hi!
I have a custom inspector (UnityEditor.Editor) for my script, and I have a SerializedProperty that is referenced by the owner of the editor.
This field works correctly in the inspector, BUT when I click on this field I get NullReferenceException (at this point field has a value, at least visually).
Why is it happening? and how to fix it?
Code examples:
public class TestClass : MonoBehaviour
{
public Transform TestTranform;
}
[CustomEditor(typeof(TestClass))]
internal class TestClassEditor : UnityEditor.Editor
{
SerializedProperty TestTranformProperty;
private void OnEnable()
{
TestTranformProperty = serializedObject.FindProperty("TestTranform");
}
public override void OnInspectorGUI()
{
EditorGUILayout.PropertyField(TestTranformProperty);
serializedObject.ApplyModifiedProperties();
}
}
NullReferenceException
NullReferenceException: Object reference not set to an instance of an object
UnityEditor.ProjectBrowser.FrameObject (System.Int32 instanceID, System.Boolean ping) (at <83719566e1474cd8800c02cd178db127>:0)
UnityEditor.EditorGUIUtility.PingObject (System.Int32 targetInstanceID) (at <83719566e1474cd8800c02cd178db127>:0)
UnityEditor.EditorGUIUtility.PingObject (UnityEngine.Object obj) (at <83719566e1474cd8800c02cd178db127>:0)
UnityEditor.EditorGUI.PingObjectOrShowPreviewOnClick (UnityEngine.Object targetObject, UnityEngine.Rect position) (at <83719566e1474cd8800c02cd178db127>:0)
UnityEditor.EditorGUI.DoObjectField (UnityEngine.Rect position, UnityEngine.Rect dropRect, System.Int32 id, UnityEngine.Object obj, UnityEngine.Object objBeingEdited, System.Type objType, System.Type additionalType, UnityEditor.SerializedProperty property, UnityEditor.EditorGUI+ObjectFieldValidator validator, System.Boolean allowSceneObjects, UnityEngine.GUIStyle style, UnityEngine.GUIStyle buttonStyle, System.Action`1[T] onObjectSelectorClosed, System.Action`1[T] onObjectSelectedUpdated) (at <83719566e1474cd8800c02cd178db127>:0)
UnityEditor.EditorGUI.DoObjectField (UnityEngine.Rect position, UnityEngine.Rect dropRect, System.Int32 id, UnityEngine.Object obj, UnityEngine.Object objBeingEdited, System.Type objType, UnityEditor.SerializedProperty property, UnityEditor.EditorGUI+ObjectFieldValidator validator, System.Boolean allowSceneObjects, UnityEngine.GUIStyle style) (at <83719566e1474cd8800c02cd178db127>:0)
UnityEditor.EditorGUI.DoObjectField (UnityEngine.Rect position, UnityEngine.Rect dropRect, System.Int32 id, System.Type objType, UnityEditor.SerializedProperty property, UnityEditor.EditorGUI+ObjectFieldValidator validator, System.Boolean allowSceneObjects, UnityEngine.GUIStyle style) (at <83719566e1474cd8800c02cd178db127>:0)
UnityEditor.EditorGUI.ObjectFieldInternal (UnityEngine.Rect position, UnityEditor.SerializedProperty property, System.Type objType, UnityEngine.GUIContent label, UnityEngine.GUIStyle style, UnityEditor.EditorGUI+ObjectFieldValidator validator) (at <83719566e1474cd8800c02cd178db127>:0)
UnityEditor.EditorGUI.DefaultPropertyField (UnityEngine.Rect position, UnityEditor.SerializedProperty property, UnityEngine.GUIContent label) (at <83719566e1474cd8800c02cd178db127>:0)
UnityEditor.PropertyHandler.OnGUI (UnityEngine.Rect position, UnityEditor.SerializedProperty property, UnityEngine.GUIContent label, System.Boolean includeChildren, UnityEngine.Rect visibleArea) (at <83719566e1474cd8800c02cd178db127>:0)
UnityEditor.PropertyHandler.OnGUI (UnityEngine.Rect position, UnityEditor.SerializedProperty property, UnityEngine.GUIContent label, System.Boolean includeChildren) (at <83719566e1474cd8800c02cd178db127>:0)
UnityEditor.PropertyHandler.OnGUILayout (UnityEditor.SerializedProperty property, UnityEngine.GUIContent label, System.Boolean includeChildren, UnityEngine.GUILayoutOption[] options) (at <83719566e1474cd8800c02cd178db127>:0)
UnityEditor.EditorGUILayout.PropertyField (UnityEditor.SerializedProperty property, UnityEngine.GUIContent label, System.Boolean includeChildren, UnityEngine.GUILayoutOption[] options) (at <83719566e1474cd8800c02cd178db127>:0)
UnityEditor.EditorGUILayout.PropertyField (UnityEditor.SerializedProperty property, UnityEngine.GUIContent label, UnityEngine.GUILayoutOption[] options) (at <83719566e1474cd8800c02cd178db127>:0)
TestClassEditor.OnInspectorGUI () (at Assets/TestClassEditor.cs:309)
UnityEditor.UIElements.InspectorElement+<>c__DisplayClass71_0.<CreateIMGUIInspectorFromEditor>b__0 () (at <83719566e1474cd8800c02cd178db127>:0)
UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr, Boolean&)
Unity version 2022.2.3f1