I am trying to build up a health system that makes it easy to plug in standard damage behaviour like adding/removing health. I want to make it so that when the objects health reaches 0 or below then the generic health script calls custom Kill functionality. So for example, if we kill the player then the scene reloads but if we kill an enemy then I can start up an explosion particle effect. I am trying to achieve this by providing a pluggable IKillable interface to my generic health management system.
I know that I can’t serialize an interface and have it pluggable in the inspector so my question is what is the generally accepted way of composing objects in this way? Do I have to create a script for each killable component whose sole responsibility is to initialize the health system with custom kill functionality? Am I better off removing the kill functionality from the health system and have each killable component manage their own death trigger?
Health system for reference:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Health : MonoBehaviour
{
[SerializeField] int maxHealth;
[SerializeField] int currentHealth;
[SerializeReference] IKillable deathHandler;
void Start(){
currentHealth = maxHealth;
}
void DecreaseHealthBy(int amount){
SetHealth(currentHealth - amount);
}
void IncreaseHealthBy(int amount){
SetHealth(currentHealth + amount);
}
void SetHealth(int newCurrentHealth){
currentHealth = newCurrentHealth;
if(deathHandler != null && currentHealth <= 0){
deathHandler.Kill();
}
}
}