So trying to do the above. To be more specific, my character shoots an arrow, if it hits the enemy’s collider, I want him to take damage. Fairly straight forward. Method I am using is:
hit.collider.SendMessageUpwards("ApplyDamage", arrowDamage, SendMessageOptions.DontRequireReceiver);
I have a variable “hit” set as a GameObject and I set my arrow as that game object in the inspector. Then in my health script I have a function, ApplyDamage, which applies the damage to the enemy. Problem is, the enemy is not losing health. Where did I go wrong? Any thoughts?
Here is some more information:
playerAbility script where I send the message after I shoot the arrow:
function Update ()
{
if(Input.GetButtonUp("Fire1")) // shoot arrows
{
ShootArrow();
hit.collider.SendMessageUpwards("ApplyDamage", arrowDamage, SendMessageOptions.DontRequireReceiver);
Debug.Log("You hit an enemy");
}
}
Then here is my function in the enemy health script:
function Applydamage (damage : float)
{
if(curHealth <= 0)
return;
else if(curHealth >0)
{
curHealth -= damage;
Debug.Log("Enemy was hit");
}
}