- Home /
Question by
tpylypenko · Jun 10, 2020 at 01:37 PM ·
interfacemonobehaviorsubclasssubclassing
How to have a generally usable class call an abstract method
I have several GameObjects which have the script HealthManager
public class HealthManager : MonoBehaviour
{
public int health;
public int maxHealth;
public DeathHandler deathHandler;
void Update()
{
if(health <= 0)
{
deathHandler.Die();
}
}
}
I have DeathHandler as its own thing because different GameObjects do different things when they die. Originally, I tried to do this using a DeathHandler interface, but it won't let interfaces be MonoBehavior so I can't use the serializability to pass in a script that implements DeathHandler.
I thought that the alternative would be to make DeathHandler an abstract class because then you can at least pass stuff in, but for some reason it won't let me make a subclass that also implements MonoBehavior
public class SoldierDeathHandler : DeathHandler, MonoBehavior // <- This is an error
{
public void Die()
{
//code
}
}
How am I supposed to do this? I don't want to write spaghetti code that checks for every single GameObject type that can hold a HealthManager
Comment