I’m currently working on making my own custom editor, and I having a problem dealing with [field: SerializeField] properties.
Below is my ScriptableObject script that I’m trying to write my own custom editor and some of the custom editor script I wrote.
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
[CreateAssetMenu(fileName = "newCombatAbilityData", menuName = "Data/Combat Ability Data")]
public class CombatAbility : ScriptableObject
{
[field: SerializeField] public Sprite combatAbilityIcon { get; private set; }
[field: SerializeField] public string combatAbilityName { get; private set; } = "Default Combat Ability Name";
[field: SerializeField, TextArea] public string combatAbilityDescription { get; private set; } = "Default Combat Ability Description";
[field: SerializeField] public int staminaCost { get; private set; }
[field: SerializeField] public Vector3Int castingRange { get; private set; }
public Dictionary<Vector3Int, bool> castingRangeDictionary { get; private set; }
[field: SerializeField] public Vector3Int AOE { get; private set; }
public Dictionary<Vector3Int, bool> AOEDictionary { get; private set; }
[field: SerializeReference] public List<CombatAbilityComponent> combatAbilityComponents { get; private set; }
public void AddComponent(CombatAbilityComponent componentData)
{
if (combatAbilityComponents.FirstOrDefault(type => type.GetType().Equals(componentData.GetType())) == null)
{
combatAbilityComponents.Add(componentData);
}
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using UnityEditor;
using UnityEditor.Callbacks;
using UnityEngine;
[CustomEditor(typeof(CombatAbility))]
public class CombatAbilityEditor : Editor
{
[SerializeField] private Texture2D gridCellTexture;
private const float maxGridCellLength = 25.0f;
private const float gridCellInterval = 2.0f;
private static List<Type> combatAbilityComponentTypes = new List<Type>();
private bool showAddCombatAbilityComponentsButtons;
private bool hasValueInDictionary;
private CombatAbility combatAbilityData;
private SerializedProperty combatAbilityIcon;
private SerializedProperty combatAbilityName;
private SerializedProperty combatAbilityDescription;
private SerializedProperty staminaCost;
private SerializedProperty castingRange;
private SerializedProperty AOE;
private SerializedProperty combatAbilityComponents;
private void OnEnable()
{
combatAbilityData = target as CombatAbility;
combatAbilityIcon = serializedObject.FindProperty("combatAbilityIcon");
combatAbilityName = serializedObject.FindProperty("combatAbilityName");
combatAbilityDescription = serializedObject.FindProperty("combatAbilityDescription");
staminaCost = serializedObject.FindProperty("staminaCost");
castingRange = serializedObject.FindProperty("castingRange");
AOE = serializedObject.FindProperty("AOE");
combatAbilityComponents = serializedObject.FindProperty("combatAbilityComponents");
}
public override void OnInspectorGUI()
{
// Do something
}
I tried to fetch properties by using serializedObject.FindProperty(“propertyName”) in OnEnable, but I’m getting a error saying that
“NullReferenceException: Object reference not set to an instance of an object
UnityEditor.EditorGUILayout.IsChildrenIncluded (UnityEditor.SerializedProperty prop) (at <0ab653b677424c84aa7f1e3350b765bc>:0)”
I found out that simply using public or [SerializeField] works well with the code above, [field: SerializeField] is causing problem.
Is there any way that I can fix this?
Thank you in advance.