I have a base class called Weapon and several derived classes e.g. Weapon_Rifle, Weapon_Pistol etc.
In another class I need to reference whatever derived class of Weapon exists on the GameObject in question. In pseduocode what I’m trying to do is this:
public class OtherClass : MonoBehaviour
{
public string weaponType;
public SCRIPT_I_WANT_TO_REFERENCE;
void Awake(){
if(weaponType =="Rifle"){
SCRIPT_I_WANT_TO_REFERENCE = GetComponent<Weapon_Rifle>();
} else {
SCRIPT_I_WANT_TO_REFERENCE = GetComponent<Weapon_Pistol>(); // etc etc
}
}
void DoStuff(){
SCRIPT_I_WANT_TO_REFERENCE.ShootWeapon();
}
}
Trouble is of course I can’t use a dummy type like Object for the SCRIPT_I_WANT_TO_REFERENCE as the compiler complains where I try to access methods of that script like ShootWeapon().
Any way this can be done?
Many thanks.