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!

So you're calling SendMessage, on the gameObject of hit.transform. Are you sure that the GameObject that is the parent of hit.transform has the script with ApplyDamage attached to it?

–

1 Answer

1

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.

To expand, on the second part. You'll be better off not calling GetComponent() twice and instead assigning it to a local variable: YourScriptName scriptInstance = hit.transform.GetComponent<YourScriptName>(); if (scriptInstance != null) { scriptInstance.ApplyDamage(Damage); Debug.Log("Damage."); }

–

So after testing the first and second lines of code, the debug is no longer called in the console. So I'm assuming that a different parent/child like you said is receiving the message and not the Enemy (which has no children objects, since I'm only using a test enemy). And I knew SendMessage was a heavy call since it uses reflection. It's really all I found at first. I do also have another weapon in my scene with basically the same code as the one above, could that possibly be interfering? It's disabled when the main is activated so I would assume that it isn't. Thanks!

–