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.