I want to create a custom attribute for serialize an interface to the inspector. So far, I already show the object field with type of interface to the inspector, but if I drag and drop a component to that field I got an error that say “InvalidOperationException: Cannot assign an object deriving from UnityEngine.Object to a managed reference. This is not supported”. Anyone know how to solve this problem?
You cannot solve this problem. Interfaces cannot be dragged onto Inspector references.
Instead, make a base class that implements the interface, or just rely on the base class. And add the [SerializeReference] attribute to the field, otherwise deserialization will fail.
OP, if you’re going to post code, use code tags and not screenshots.
Tons of people have solved this problem. There’s at least a dozen solutions out there.
SerializeReference is also only for non-Unity object types. It’s not meant to serialize references to Unity objects.
1 Like
Thanks for the clarifications!
Clearly I haven’t used any of these often enough and I tend to think that’s because there is always an alternative that just plays better with Unity and still works well.