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.
StandardShip.cs
public class StandardShip : PlayerShip<LaserBullet> {...}
PlayerShip.cs
public abstract class PlayerShip<T> : Ship where T : Bullet {...}
LaserBullet obviously inherits from Bullet, which in turn inherits from Monobehaviour too.
Bullet.cs
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.
Player.cs
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.