property.FindPropertyRelative("property") works only for attributes but not actual properties?

I have this code in a PropertyDrawer:

property.FindPropertyRelative("propertyFloatValue").floatValue = X; // returns correct value

That does its job if the “propertyFloatValue” is this:

public float propertyFloatValue;

But I need to do some input control and assign a variable when propertyFloatValue is assigned, so I’ve transformed that variable in a Property:

public float propertyFloatValue { get; set; }

But for some reasons this now returns null.

property.FindPropertyRelative("propertyFloatValue").floatValue = X; // now returns null

Any help would be greatly appreciated! Thanks!

SerializedObjects and SerializedProperties use Unity’s default serialization, which doesn’t support properties (somewhat of a misnomer, I know).

1 Like

ohw :frowning:

Using SerializedObject and SerializedProperty, you’re editing the serialized data directly, not the actual object instances. Therefore you can only edit what Unity actually serializes, i.e. what is visible in the inspector.

You can have a private field and decorate it with [SerializeField] to expose in the inspector and make it accessible with SerializedProperty. Then add a public property that uses that field to use in your code.

If you want to do edit-time validation, you’re better off doing that directly in the property drawer. There’s no way to reliably check scripts in the editor, so you’ll have to repeat the checks when the script is loaded anyway (e.g. there’s no way to re-validate all scripts when you change the requirements, so existing scripts might end up with invalid values).