Nested UnityEvent subclass inside of a Generic class.

public class BaseClass<T> : ScriptableObject
{
    [System.Serializable]
    private class UnityEventType : UnityEvent<T> {}

    [SerializeField]
    private UnityEventType m_event;
}

[CreateAssetMenu]
public class SubclassBool : BaseClass<bool> {}

The m_event field is not shown in the inspector. I can define a separate UnityEvent subclass for every use case subclass manually, and it will work, but I don’t want to have such boilerplate code as I would have many subclasses.

My question is: Why this doesn’t work?

1 Like

Hey there,

I also had the same problem. A solution I came up with that reduces the boilerplate is:

public class GenericEvent<TType, TEvent> : MonoBehaviour where TEvent : UnityEvent<TType>
{
    public TEvent MyUnityEvent;
}

[Serializable]
public class IntUnityEvent : UnityEvent<int> {}

public class MyTypedEvent : GenericEvent<int, IntUnityEvent> { }