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