Trouble Creating Field In Custom Inspector

I’m just getting into creating custom inspectors, and feeling pretty clueless in the process. Here’s what I want to do currently:

  • Create a field in my custom inspector for LevelBuilder that accepts Tilemaps from within the scene
  • Access the reference stored in that field whenever I want, such as on a GUI button click

Here’s my current custom inspector code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEngine.Tilemaps;

[CustomEditor(typeof(LevelBuilder))]
public class LevelBuilderEditor : Editor
{
    public override void OnInspectorGUI()
    {
        base.OnInspectorGUI();

        LevelBuilder levelBuilder = (LevelBuilder)target;

        EditorGUILayout.Space();
        EditorGUILayout.LabelField("", GUI.skin.horizontalSlider);
        EditorGUILayout.Space();
        EditorGUILayout.PropertyField(serializedObject.FindProperty("TilemapToWriteEditor"));

        if (GUILayout.Button("Write Tilemap To Asset"))
        {
            Debug.Log(levelBuilder.TilemapToWriteEditor);
            //levelBuilder.WriteTilemapToAsset();
        }
    }
}

The custom field is not working, and is instead flooding my console with the following error:

UnityEditor.EditorGUILayout.IsChildrenIncluded (UnityEditor.SerializedProperty prop) (at <afa5b9a1793446ff98b741dc036c4c6e>:0)
UnityEditor.EditorGUILayout.PropertyField (UnityEditor.SerializedProperty property, UnityEngine.GUILayoutOption[ ] options) (at <afa5b9a1793446ff98b741dc036c4c6e>:0)
LevelBuilderEditor.OnInspectorGUI () (at Assets/Scripts/Editor/LevelBuilderEditor.cs:19)
UnityEditor.UIElements.InspectorElement+<>c__DisplayClass58_0.<CreateIMGUIInspectorFromEditor>b__0 () (at <afa5b9a1793446ff98b741dc036c4c6e>:0)
UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr)```

The error clearly indicates that there's a null reference in line 19, but I'm not sure what's actually null. I honestly don't understand what serializedObject is (I wrote that line while trying to follow a video tutorial), but I did a Debug.Log on it just before that line, and it didn't print "Null" in the console.

You are trying to find this property “TilemapToWriteEditor” and the code can’t find it.

So what is that? Where did you got that property from? Why you need it?

You can start by browsing this API reference

maybe you can find there a solution

The property is inside my LevelBuilder class. I copied and pasted the name of the property so there wouldn’t be any spelling mistakes. Here’s the property itself:

    public Tilemap TilemapToWriteEditor
    {
        get;
        set;
    }

If I were trying to access the property under “normal” circumstances, there would be no issues. But for some reason, in this case, it can’t be found?

Also, I’ve already looked through that documentation, and couldn’t find the answer. What I actually tried first was using ObjectField, but that didn’t work for me, either.