Hey all. I’m following a tutorial on making a hex grid, and on one of the steps the author gets some properties using the FindPropertyRelative() function. However, when I do the exact same thing I get null a reference exception.
The tutorial: Hex Map 1
Here’s the bit of code from the tutorial:
using UnityEngine;
using UnityEditor;
[CustomPropertyDrawer(typeof(HexCoordinates))]
public class HexCoordinatesDrawer : PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
HexCoordinates coordinates = new HexCoordinates(property.FindPropertyRelative("x").intValue, property.FindPropertyRelative("z").intValue);
position = EditorGUI.PrefixLabel(position, label);
GUI.Label(position, coordinates.ToString());
}
}
From what I gather, what’s supposed to happen is that I click on a HexCell gameobject in the editor and that object is serialized and passed into the OnGUI() function as the property argument. The FindPropertyRelative() function then looks into this and finds the fields “x” and “z”, changes them to int values, and passes them into the constructor for the coordinates struct. Inside the coordinates struct is a ToString() method that converts the fields to a string, which is used in the GUI.Label() method to pass the into to the editor to be shown.
My questions:
- Why is the FindPropertyRelative() function giving me a null reference error?
- On a somewhat related note, where does the info for the value of the Rect position parameter come from? Not crucial, just curious.