I saw a lot of similar questions but non of them helped me, and they were old too. So I want to ask that in the script below when kill an enemy, automatically the currentHelath of all enemies go down. Can someone tell me what is wrong with the script?
Note: currentHealth is a non-static variable defined in characterStats class. and I call the fire method in PlayerGun script.
void Fire()
{
muzzleFlash.Play();
gunSound.Play();
currAmmo--;
animator.SetInteger("condition", 6);
RaycastHit hit;
if (Physics.Raycast(transform.position, Camera.main.transform.forward, out hit, database.weapons[id].range))
{
nextTimeToFire = Time.time + 1f/database.weapons[id].fireRate;
if (hit.transform.tag == "Enemy")
{
CharacterStats enemyStat = hit.transform.GetComponent<CharacterStats>();
if (!enemyStat.isDead)
{
enemyStat.TakeDamage(database.weapons[id].damage);
if (enemyStat.currentHelath <= 0)
{
enemyStat.isDead = true;
coins = coins + 10;
coinsText.text = coins.ToString();
enemyAnim.SetTrigger("isDeadTrigger");
}
}
}
if (hit.rigidbody != null)
{
hit.rigidbody.AddForce(-hit.normal * impactForce);
}
GameObject impactGO = Instantiate(impactEffect, hit.point, Quaternion.LookRotation(hit.normal));
Destroy(impactGO, 1f);
}
}