Getting Null Reference When Using FindPropertyRelative()

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:

  1. Why is the FindPropertyRelative() function giving me a null reference error?
  2. On a somewhat related note, where does the info for the value of the Rect position parameter come from? Not crucial, just curious.

Because there is no serialized property by that name. So either you don’t have a serialized value by that name, or the field with that name isn’t being serialized.

Maybe you missed the part of the tutorial where they set up serialized fields for x and z?

From Unity’s side of things.

Hmm. Not sure. I click on a HexCell object in the editor to run the above code, and this HexCell gameobject has the HexCell script attached to it (it’s the only thing attached). Here’s the script:

public class HexCell : MonoBehaviour
{
    public HexCoordinates coordinates;
    
    [SerializeField]
    private int x, z;
    public int X { get { return x; } }
    public int Z { get { return z; } }

    public void HexCoordinates (int x, int y)
    {
        this.x = x;
        this.z = z;
    }
    
}

Going through the debugging in Visual Studio seemed to indicate that this was the script that was being referenced with the OnGUI() function. I’ve tried using capital X and Z instead, and turning x and z public, but I still get a null reference either way.

You’ve bungled up your code. You’ve been writing the code for HexCoordinates in the wrong class, in HexCell instead.

Go back and follow the tutorial more closely.

1 Like

Son of a…
You are correct. Unfortunately the tutorial doesn’t label which code blocks belong to which script, leading to my confusion. Thanks for the help!