-3

I am trying to make a custom editor in Unity for a ScriptableObject class which has private fields like such:

ScriptableObject class

But has soon as one of my fields has an accessor (I also tried properties with get;), I get the following error when I try to see my ScriptableObject in the inspector.

NullReferenceException

I made some tests and it works perfectly without accessors. For instance, I can see the field "test". Here is the code for my custom editor:

Custom editor script

Any idea? I would not believe a custom editor for a class that has accessors would not be supported. Thank you!

derHugo
  • 83,094
  • 9
  • 75
  • 115
Cappic
  • 9
  • 6
  • 1
    Does this answer your question? [In Unity (C#), why am I getting a NullReferenceException and how do I fix it?](https://stackoverflow.com/questions/62413907/in-unity-c-why-am-i-getting-a-nullreferenceexception-and-how-do-i-fix-it) – Peter Duniho Apr 19 '21 at 02:09
  • 2
    Please [**don't post images of code**](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-errors-when-asking-a-question) .. – derHugo Apr 19 '21 at 09:12
  • Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Peter Duniho Jul 17 '21 at 16:58

1 Answers1

1

This has nothing to do with an "accessor". And note that properties are not serialized by Unity at all.


The mistake is pretty simple: Your field's name is not ingredients but rather _ingredients!

Therefore FindProperty("ingredients") returns null since it doesn't find any field called ingredients.

(This is assuming of course that Item is a [Serializable] type at all.)


To avoid exactly this type of issues I usually prefer to embed the editor into the type itself and use e.g.

public class YourType : MonoBehaviour /*or ScriptableObject*/
{
    [SerializeField] private int _someField;

#if UNITY_EDITOR
    [CustomEditor(typeof(YourType))]
    private class YourTypeEditor : Editor
    {
        private SerializedProperty _someFieldProperty;

        private void OnEnable()
        {
            _someFieldProperty = serializedObject.FindProperty(nameof(_someField));
        }

        ...
    }
#endif
}

so whenever you rename the fields it will be fixed in the Inspector automatically

derHugo
  • 83,094
  • 9
  • 75
  • 115
  • Also. From the first part. No list is made so the list can be null – BugFinder Apr 19 '21 at 10:14
  • @BugFinder the list is a `[SerializeField]` so Unity's serializer will automatically initialize it .. assuming of course that `Item` is actually a serializable type ;) – derHugo Apr 19 '21 at 10:55
  • Thank you for noticing that, I can't believe I made such mistake... Also sorry for putting images of my codes, I'll do that more properly next time. – Cappic Apr 19 '21 at 15:09
  • This fixed my issue... I had renamed the field inside the class for a property drawer. – ow3n Aug 22 '22 at 16:14