Compoment Function Access

Hello.
Let’s say i’ve two compoments of enemytypes, a Melle script, and a Ranged script,
every two of them have health. How to call DealDamage function indenpendently on a compoment? Without SendMessage

Maybe i should use class inheritance? or what?

Thanks in advance

If I understand your question, then yes, you can use inheritance. You can have a base class

public class EnemyType : MonoBehaviour
{
    public virtual void DealDamage()
    {
        print("deal damage base");
    }
}

and two derived classes

public class Melee : EnemyType
{
    public override void DealDamage()
    {
        base.DealDamage();
        print("DD melee");
    }
}

public class Ranged : EnemyType
{
    public override void DealDamage()
    {
        base.DealDamage();
        print("DD ranged");
    }
}

If you attach one of these scripts to game object enemy1, you can call DealDamage using

var enemyType = enemy1.GetComponent<EnemyType>();
enemyType.DealDamage();