Enemy and gun damge script not working.

I have a enemy script and a gun damage script which uses raycasting but my enemy wont take damage and loose health can someone please check whats wrong with this script.
Enemy script

     using System.Collections;
     using System.Collections.Generic;
     using UnityEngine;

public class EnemyScript : MonoBehaviour
{
    public int EnemyHealth = 10;

void deductPoints(int DamageAmount)
{
    EnemyHealth -= DamageAmount;
}

void Update()
{
    if (EnemyHealth <= 0)
    {
        Destroy(gameObject);
    }
}

}

Gun damage script

  using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;

public class HandGunDamage : MonoBehaviour
{
public int DamageAmount = 5;
public float TargetDistance;
public float AllowedRange = 15.0f;

    void Update()
   {
    if (Input.GetButtonDown("Fire1"))
    {
        RaycastHit Shot;
        if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out Shot))
        {
            TargetDistance = Shot.distance;
            if (TargetDistance < AllowedRange)
            {
                Shot.transform.SendMessage("DeductPoints", DamageAmount);
            }
        }
    }
}

}
Please help me i have been searching a solution of this for some time now but i cant find a fix for this and i know there was a similiar post like this but i didnt help me. Thanks in advance.

,

Divinitize1 is right, you have a big flaw in how you call your function on the enemy. SendMessage should be called only when prototyping.

Instead, you should simply check whether the hit gameObject (or one of its children) has the desired component and having a tag on the enemy will reduce unnecessary calls to GetComponent.

So tag your enemies gameObjects (and all their children) Enemy and change your weapon code to:

void Update()
{
    if (Input.GetButtonDown("Fire1"))
     {
         RaycastHit shot;
         bool hittingEnemy = Physics.Raycast(transform.position, transform.forward, out shot) &&
             shot.distance < AllowedRange &&
             shot.transform.CompareTag("Enemy") ;
         if (hittingEnemy)
         {
             EnemyScript enemy = GetEnemyScript( shot.transform );
        
             if (enemy != null)
             {
                 enemy.DeductPoints(DamageAmount);
             }
             else
             {
                  Debug.LogError("An enemy does not have the EnemyScript component attached");
             }
         }
     }
}

private EnemyScript GetEnemyScript( Transform hitObject )
{
    EnemyScript enemy = hitObject.GetComponentInChildren<EnemyScript>();
    while( enemy == null && hitObject.CompareTag("Enemy"))
    {
         hitObject = hitObject.parent;
         enemy = hitObject.GetComponent<EnemyScript>();
    }
    return enemy;
}

let you try with event system? i think that might good choice in this case.