Send Message to Object I've hit

Hi,

Having a bit of a blind moment and can’t figure out how to only Send a message to the object i’m hitting.

I’m using the below and once one enemy is dead the other doesn’t take any damage. Both objects are tagged ‘Enemy’ and it appears like the script on both enemies are receiving the ‘ApplyDamage’, when i only want the one i’m attacking too.

Many Thanks

function damage ()
{
  var hit : RaycastHit; 

  if (Physics.Raycast(transform.position, transform.forward, hit))
  {
    if (hit.collider.gameObject.CompareTag("Enemy"))
    { 
       if(CharacterDamage.hitPoints >0) 
         { 
           hit.collider.SendMessageUpwards ("ApplyDamage", 25, SendMessageOptions.DontRequireReceiver);
           yield WaitForSeconds(0.4);
           audio2.Play();
         }   
    }
  }
}

have a look at:

SendMessageUpwards looks like it works within the gameobject it’s attached to,

SendMessage works on another gameobject

i.e. I think they are hitting the other target but taking an amount off their own health… (try disabling one of the scripts and see which health decreases to check i guess)

try using SendMessage to fix.

Thanks. That message up was a typo by me and i’ve removed it now.

Still the same issue.

It only appears to work on one enemy, but i don’t know why or how to resolve it.

Thanks

Sorry, this actually works now. So thanks for your help :slight_smile: