Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Property not displaying in inspector, even with [field: SerializeField]

I’ve started using [field: SerializeField] to serialize properties so they show up in the inspector, like so:

[field: SerializeField] public int foo { get; private set; }

However, when I add functionality to the the property, it stops working.

private int _bar;

[field: SerializeField] 
public int bar
{
     get { return _bar; }
     set 
     { 
          _bar = value;
          DoThing();
     }
}

The keyword field has a green underline with the following warning:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

"field" is not a valid attribute location for this declaration. 
Valid attribute locations for this declaration are 'property'. 
All attributes in this block will be ignored.

This seems to be disabling the attribute altogether, meaning it doesn’t show up in the inspector.

I’ve tried replacing it with [property: SerializeField] like it suggests, but this doesn’t show up in the inspector, nor can I find anything about it online.

I’m aware that I can create a custom inspector that will display it, but it’s a lot of extra effort for what I’m trying to do, and I want to avoid it if possible.

Is there a way to get a property like this to display in the inspector without a custom editor?

>Solution :

You can’t serialize properties in Unity.

Serialize your backing field "_bar" instead. Or write a custom editor for that object type if you want more control in the inspector.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading