Force custom PropertyDrawer to trigger OnValidate()

I have a PropertyDrawer for a custom Serializeable class type, and can edit and change it fine. However I’ve run into a snag where since I’m changing the property using fieldInfo with GetValue() and SetValue(), it doesn’t seem to trigger OnValidate() on the target object.

Undo.RecordObject() works fine, so I can undo the changes, but even after calling property.serializedObject.SetIsDifferentCacheDirty() and property.serializedObject.Update() I still don’t get OnValidate() called.

I’m going to assume this isn’t working because the serializedObject is detecting changes, since you’re modifying those values using reflection rather than through the serializedObject/serializedProperties.

maybe try calling it manually yourself.

MonoBehaviour target = property.serializedObject.targetObject as MonoBehaviour;

if (target) {

    target.SendMessage("OnValidate", null, SendMessageOptions.DontRequireReceiver);
}
4 Likes