Managing taking damage.

Supposing we have different objects in our game, ships, bases, turrets, cargo pods etc. and projectiles, when a projectile hits something, how will it know what function to call in order for that object to take damage? Knowing that each object has a special manager, that implements an interface IDamageable let’s say, and that it’s always at the root of the object (but turrets will be children for example) and the collider isn’t always in the same object as the manager, how can I handle the damage that the projectile does (along with other special attacks etc)? Also, the managers of each object would inherit from a base manager(if that helps at all)…

Why not implement from Monobehaviour base? On hit try to get the component, if it’s not null then remove health. Here’s and example.

public class KillableBase: MonoBehaviour {

    [SerializeField]private int _health = 100;

    public void RemoveHealth(int amount){
        health -= amount;
    }

}

public class Ship: KillableBase{
    //members and methods for ship only
}
  1. Now our ship is somewhere in the game

  2. It gets hit.

       int damage = 57;
    GameObject hitObject; //get our hit from raycast
    
    KillableBase killable = hitObject.getComponent<KillableBase>();
    
    if(killable  != null){
        killable.RemoveHealth(damage );
    }