enemy not dies when player attacks and health value not Decreasing.

Enemies not dying and their health not decreasing, when player attacks them .There is scripts below for player attack(Gun.cs) and enemy health(Adg.cs) .pls solve if my code is wrong?!

Gun.cs

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

public class Gun : MonoBehaviour
{

protected PlayerController playerController;
public FixedButton Button;
[SerializeField]
[Range(0.1f, 1.5f)]
private float fireRate = 0.1f;

private int damage = 1;

[SerializeField]
private ParticleSystem muzzleParticle;

[SerializeField]
private AudioSource gunFireSource;

private float timer;
// Update is called once per frame
void start()
{
    
    playerController = GetComponent<PlayerController>();
    playerController.SetArsenal("Rifle");
}
void Update()
{

    timer += Time.deltaTime;
    if(timer >=fireRate)
    {
        if (Button.Pressed)
        {
            
            timer = 0f;
            
            FireGun();
        }
    }
}
private void FireGun()
{
   

    muzzleParticle.Play();
    gunFireSource.Play();

    Ray ray = Camera.main.ViewportPointToRay(Vector3.one * 0.5f);

    Debug.DrawRay(ray.origin, ray.direction * 100, Color.red, 2f);

    RaycastHit hitInfo;

    if (Physics.Raycast(ray, out hitInfo, 100))
    {
        var health = hitInfo.collider.GetComponent<adg>();

        if (health != null)
            health.TakeDamage(damage);
    }
} 

}

Adg.cs

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

public class adg : MonoBehaviour
{
public int level;
private AudioSource acf;
private OneThirdPersonController Chh;
private Gun gun;
public float restartDelay = 1f;

private NavMeshAgent nav;
private bnm remov;
private SphereCollider sph;
private mkl fbh;

private Animator animator;
[SerializeField]
public float maxHealth = 10;

public float currentHealth;

public void Start()
{
    acf = GetComponent<AudioSource>();
    Chh = GetComponent<OneThirdPersonController>();

    nav = GetComponent<NavMeshAgent>();
    sph = GetComponent<SphereCollider>();
    remov = GetComponent<bnm>();
    fbh = GetComponent<mkl>();
    animator = GetComponent<Animator>();
    currentHealth = maxHealth;
}

public void TakeDamage(int damageAmount)
{
    int i = 0;

    currentHealth -= damageAmount;
    if (currentHealth <= i)
    {
        Die();
    }

}

public void Die()
{

    animator.SetTrigger("Die");
    FindObjectOfType<GameManager>().EndGame();

   

    nav.enabled = false;
    remov.enabled = false;
    sph.enabled = false;
    fbh.enabled = false;

}

}

We don’t know how you scene and objects are set up. So instead of solving your issues I will tell you how you can debug it and find the answer yourself.

Here is what suppose to happen:

  1. timer >= fireRate
  2. Button is pressed
  3. FireGun() method is invoked
  4. Ray is fired
  5. Ray is hitting the object
  6. Ray gets the component
  7. If the component is found TakeDamage method is invoked
  8. If current health <= 0 Die is invoked

Make sure that this is what happens. And if not then you can investigate further. For example, if ray doens’t hit the object make sure that the ray is shooting towards the obejct, if the object is close enough etc.