SendMessage not calling function in other script

I do understand that questions like this have been asked before, but I’ve looked at many of them with no fix to my issue. I have a raycast that is calling a function through SendMessage and apply damage to an enemy.

The first code is

function AttackDamage ()
{
    //Attack function
    var hit : RaycastHit;
    if (Physics.Raycast(TheSystem.transform.position, TheSystem.transform.TransformDirection(Vector3.forward), hit))
    {
        Distance = hit.distance;
        if (Distance < MaxDistance)
        {
            hit.transform.SendMessage("ApplyDamage", Damage, SendMessageOptions.DontRequireReceiver);
            Debug.Log("Damage");
        }
    }
}

and the code with the function I want to call is

var Health = 100;

function Update () 
{
    if(Health <= 0)
    {
        Dead();
    }
}

function ApplyDamage(Damage : int)
{
    Debug.Log("Damaged");
    Health -= Damage;
}

function Dead() 
{
    Destroy(gameObject);
}

The AttackDamage function does call the debug at the end so I imagine that there is not issue with that function, I think it is just SendMessage not calling the ApplyDamage function. I have also tried BroadcastMessage and that hasn’t worked either.

Ask if you need anything else from me, thanks!

Only a few possible reasons come to mind here. As mentioned above, it could be that an object without the ApplyDamage script is picking up the message (like a child, or the parent without the script).

Check this via:

if (Distance < MaxDistance)
         {
             if(hit.transform.GetComponent.<YourScriptName>()){
                   hit.transform.SendMessage("ApplyDamage", Damage, SendMessageOptions.DontRequireReceiver);
             Debug.Log("Damage");
            }
         }

This returns a boolean, which you could include in a Log. Also, SendMessage is a pretty heavy call. I’d recommend going straight for the component, since you already have the object:

if (Distance < MaxDistance){
    if(hit.transform.GetComponent.<YourScriptName>()){
        hit.transform.GetComponent.<YourScriptName>().ApplyDamage(Damage);
        Debug.Log("Damage.");
    }
}

GetComponent is somewhat heavy as well, but better than SendMessage.