Trying to send damage when a collider is hit by an arrow.

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");
  }	
}

(In response to comment needing function)here you go:

function ShootArrow()
{
		var instance : Rigidbody = Instantiate(arrow, transform.Find("ArrowFirePosition").position , transform.rotation);
		instance.name = "arrow";
		var fwd : Vector3 = transform.TransformDirection(Vector3.forward);
		instance.AddForce(fwd * powerMeter);
}

I am just not sure where to put the debug.log I guess to solve this one :frowning:

Your problem is probably, that as far as I know, your subtracting the health after returning in the Applydamage funtion, so the function is called, but not executed in the right way…

Try:
    function Applydamage (damage : float) {
      if(curHealth > 0) {
        curHealth -= damage;
      }
    }

instead :slight_smile: