Enemies wont take damage how can I fix this problem
I have a problem for some reason, I have no damage being dealt onto the enemies that I shoot at here is the script that I had used: HandGundDamage.jv:
var DamageAmount : int = 6; var TargetDistance : float; var AllowedRange : float = 16;
function Update () { if(Input.GetButtonDown("Fire1")) {
var Shot : RaycastHit; if (Physics.Raycast (transform.position, transform.TransformDirection(Vector3.forward), Shot)) { TargetDistance = Shot.distance; if (TargetDistance < AllowedRange) { Shot.transform.SendMessage("deductPoints", DamageAmount); } } } } Somebody please help as soon as possible.
Answer by gashraf · Aug 01, 2018 at 08:49 PM
Not very sure about JavaScript, but you can try the following. If out Shot throws error, then ignore the out keyword. In C#, out tells the compiler that we are expecting a return value in it (pass by reference)
Also, I hope you have set up your enemy prefab with a Monobehaviour which has a function called public void deductPoints(var damage) {
}
Here goes the code:
....
var Shot : RaycastHit;
if (Physics.Raycast (transform.position, transform.forward, out Shot, AllowedRange)) {
if (Shot.transform.tag=="Enemy") {//Assuming you have tagged them Ënemy
Shot.transform.SendMessage("deductPoints", DamageAmount);
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612172015im_/https://answers.unity.com/themes/thub/images/avi.jpg)