Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Getting NullReferenceException with PropertyDrawers

Discussion in 'Scripting' started by Garbilicious, Sep 18, 2021.

  1. Garbilicious

    Garbilicious

    Joined:
    Apr 1, 2020
    Posts:
    2
    I'm trying to create a custom PropertyDrawer for a Serializable class that holds two lists, essentially to act as a Dictionary that I can serialize in the inspector. The problem is, Unity keeps finding a NullReferenceException for the SerializedProperty.

    Here is the full error:
    I have already checked for common reasons this occurs. So, yes, I do have [Serializable] in front of the class I want to make a PropertyDrawer of; I have the properties labeled with [SerializeField]; and I'm sure the names of the class I want to make the PropertyDrawer is not mispelled anywhere.

    The Serializable Class:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using System;
    5.  
    6. [Serializable]
    7. public class VariationDictionary {
    8.     [SerializeField] private List<string> _keys;
    9.     [SerializeField] private List<int> _cellIndexes;
    10.  
    11.     public bool Contains(string key) => _keys.Contains(key);
    12.  
    13.     public int GetCellIndex(string key) {
    14.         if (!Contains(key))
    15.             return 0;
    16.         for (int i = 0; i < _keys.Count; i++) {
    17.             if (_keys[i] == key) {
    18.                 return _cellIndexes[i];
    19.             }
    20.         }
    21.         return 0;
    22.     }
    23.  
    24.     public void Add(string key, int cellIndex) {
    25.         if (Contains(key))
    26.             return;
    27.         _keys.Add(key);
    28.         _cellIndexes.Add(cellIndex);
    29.     }
    30.  
    31.     public void Remove(string key) {
    32.         if (!Contains(key))
    33.             return;
    34.         for (int i = 0; i < _keys.Count; i++) {
    35.             if (_keys[i] == key) {
    36.                 _keys.RemoveAt(i);
    37.                 _cellIndexes.RemoveAt(i);
    38.             }
    39.         }
    40.     }
    41. }
    The PropertyDrawer of the class (unfinished, for this reason):
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3.  
    4. [CustomPropertyDrawer(typeof(VariationDictionary))]
    5. public class VariationDictionaryDrawer : PropertyDrawer {
    6.  
    7.     public override float GetPropertyHeight(SerializedProperty property, GUIContent label) {
    8.         return EditorGUI.GetPropertyHeight(property.FindPropertyRelative("keys"));
    9.     }
    10.  
    11.     public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) {
    12.         EditorGUI.BeginProperty(position, label, property);
    13.  
    14.         var keysProperty = property.FindPropertyRelative("keys");
    15.  
    16.         EditorGUI.PropertyField(position, keysProperty);
    17.  
    18.         EditorGUI.EndProperty();
    19.     }
    20. }
    And, the class that has an instance of the Serializable class, if that helps:
    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using System;
    6.  
    7. [CreateAssetMenu(fileName = "New ProdigyAnimation", menuName = "Prodigy Animation/Animation")]
    8. public class ProdigyAnimation : ScriptableObject {
    9.     [SerializeField] private string _name;
    10.     [SerializeField] private int _framerate;
    11.     [SerializeField] private VariationDictionary _variations;
    12.     [SerializeField] private Cell[] _cells;
    13.  
    14.     public string Name => _name;
    15.     public int Framerate => _framerate;
    16.     public Cell[] Cells => _cells;
    17. }
    Thanks for any help!
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    "keys" != _keys

    The string has to match the variable name exactly with FindProperty.
     
    trombonaut likes this.