GetComponent returns null when using a generics class for T

So I’m making a game where I have a Player.cs script that instantiates the player ship, the player ship prefab has a StandardShip component attached to it.
The StandardShip component inherits from PlayerShip which inherits from ship and so on until eventually it inherits from MonoBehaviour.


public class StandardShip : PlayerShip<LaserBullet> {...}


public abstract class PlayerShip<T> : Ship where T : Bullet {...}

LaserBullet obviously inherits from Bullet, which in turn inherits from Monobehaviour too.

public abstract class Bullet : MonoBehaviour, IPoolableEntity {...}

The error occurs inside the Player.cs file, GetComponent returns null even thought all the components inherit from MonoBehaviour so I don’t know what I’m doing wrong.


string pathToPrefab = $"Prefabs/{PlayerPrefs.GetString(PlayerPrefsConstants.SELECTED_SHIP, null) ?? "StandardShip"}";
        GameObject objectToInstantiate = Resources.Load<GameObject>(pathToPrefab);
        PlayerShip<Bullet> ship = objectToInstantiate.GetComponent<PlayerShip<Bullet>>();
        //var s = objectToInstantiate.GetComponent<Rigidbody2D>();

The Resources.Load returns the correct prefab and I tried getting the RigidBody2D, it worked. So I don’t know what the problem is. I would really appreciate if someone could tell me what I’m doing wrong 'cause I can’t figure this out.

Probably for the same reasons:

// These work      
var a = GetComponent<StandardShip>();
var b = GetComponent<PlayerShip<LaserBullet>>() as StandardShip;

// These do not        
var c = GetComponent<PlayerShip<Bullet>>() as StandardShip;
var d = (StandardShip)GetComponent<PlayerShip<Bullet>>();

i.e. When using GetComponent StandardShip is not a PlayerShip<Bullet> it’s a PlayerShip<LaserBullet>because it doesn’t support variance in that direction.