Is there support for Components with type parameters

I’ve made a component class which used to work fine, but when I added a type parameter, the script no longer loads in Unity although everything compiles fine in VS.

Code looks as follows:

public class WeaponCarrier<TWeapon> : MonoBehaviour where TWeapon : IWeapon
{
    ...
}

If I remove the TWeapon, component loads in Unity. So is there a way to get components like this to work in Unity?

Your WeaponCarrier class can’t be serialized by Unity because of the generic. Thus, it can’t be attached to a gameObject

Your only solution (AFAIK) is to declare a sub-class of WeaponCarrier<TWeapon> with the concrete type.

public class GunWeaponCarrier : WeaponCarrier<Gun> { }

I know this breaks the advantage of the generic, but I don’t think there is any other workaround.

Then, you will be able to attach the GunWeaponCarrier to a gameObject.