Defining a public variable with 'where'

I would like to define a public, user-assignable variable that is a Behaviour. The Behaviour will be a target script to call when certain conditions are true. This is the easy part

I also want to define a specific interface to implement, so I have some known public functions to call. However, if I just ask for the interface like so:

public EventReceiver EventController;

The user can’t assign a MonoBehaviour from the editor - the only way to be able to assign is to use the following:

public Behaviour EventController;

What I’m wondering is, is there any way in C# to do something like this:

public Behaviour EventController where EventController : EventReceiver;


I know that I can check the type at runtime and deliver a message to the user if they’ve assigned an incorrect target - this is what I’ve already done. I’m just curious if there’s a better way.

you can check it in editor on assigning action instead of runtime, making incorrect assigning impossible.

‘Making a Custom Editor’ chapter

Well you could make it a base class derived from MonoBehaviour - perhaps an abstract one, that would allow editor assignment - but not the query for an interface.