-2

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
  • 68,790
  • 9
  • 58
  • 97
Cappic
  • 11
  • 4
  • 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
  • 1
    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

0

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.)

derHugo
  • 68,790
  • 9
  • 58
  • 97
  • 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