- Home /
Enemy Damage
It has been 7 days, that I am trying to solve this problem, But I can not figure it out. Please help me.
I have an enemyHealth script something like this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyHealth : MonoBehaviour
{
public float maxHealth = 100f;
public float currentHealth;
public bool isDead = false;
Animator enemyAnim;
private void OnEnable()
{
currentHealth = maxHealth;
enemyAnim = GameObject.FindGameObjectWithTag("Enemy").GetComponentInChildren<Animator>();
}
public void TakeDamage(float damageAmount)
{
currentHealth -= damageAmount;
if(currentHealth <= 0)
{
Die();
}
}
public void Die()
{
enemyAnim.SetTrigger("isDeadTrigger");
isDead = true;
}
}
and then I have a playerGun script, where i shoot the enemies. it is something like this:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PlayerGun : MonoBehaviour
{
int id;
[SerializeField]
int currAmmo;
float reloadSpeed = 3;
public AudioSource source;
public AudioClip gunSound;
public AudioClip reloadSound;
float impactForce = 30f;
public float fireRate = 0.1f;
public GameObject enemyCarrate;
public int coins;
public Text coinsText;
GameObject gameController;
WeaponDatabase database;
Animator animator;
Animator enemyAnim;
EnemyHealth enemyStat;
public ParticleSystem muzzleFlash;
public GameObject impactEffect;
bool reloading = false;
private float timer;
private void Awake()
{
source = GetComponent<AudioSource>();
}
private void Start()
{
gameController = GameObject.FindGameObjectWithTag("GameController");
database = gameController.GetComponent<WeaponDatabase>();
id = GameObject.FindGameObjectWithTag("Weapon").GetComponent<ItemID>().itemID;
currAmmo = database.weapons[id].maxAmmo;
animator = GameObject.FindGameObjectWithTag("Player").GetComponentInChildren<Animator>();
enemyAnim = GameObject.FindGameObjectWithTag("Enemy").GetComponentInChildren<Animator>();
coins = 0;
}
private void Update()
{
if (Input.GetKey(KeyCode.R) && !reloading)
{
if (currAmmo == database.weapons[id].maxAmmo)
{
return;
}
else
{
StartCoroutine(Reload());
//stop firing while reloading
return;
}
}
if(currAmmo<=0)
{
StartCoroutine(Reload());
//stop firing while reloading
return;
}
timer += Time.deltaTime;
if (timer >= fireRate)
{
if (Input.GetButton("Fire1"))
{
timer = 0;
Fire();
}
}
/*
if (Input.GetButton("Fire1") && Time.time >= nextTimeToFire)
{
Fire();
}
*/
}
IEnumerator Reload()
{
reloading = true;
source.PlayOneShot(reloadSound, 0.3f);
animator.SetBool("reloading", true);
yield return new WaitForSeconds(reloadSpeed);
currAmmo = database.weapons[id].maxAmmo;
reloading = false;
animator.SetBool("reloading", false);
}
void Fire()
{
muzzleFlash.Play();
source.PlayOneShot(gunSound, 0.3f);
currAmmo--;
animator.SetInteger("condition", 6);
RaycastHit hit;
if (Physics.Raycast(transform.position, Camera.main.transform.forward, out hit, database.weapons[id].range))
{
enemyStat = hit.transform.GetComponent<EnemyHealth>();
if (hit.transform.tag == "Enemy")
{
if (!enemyStat.isDead)
{
enemyStat.TakeDamage(database.weapons[id].damage);
if (enemyStat.currentHealth <= 0)
{
enemyStat.isDead = true;
//Instantiate(enemyCarrate, enemyStat.transform.position, enemyStat.transform.rotation);
coins = coins + 10;
coinsText.text = coins.ToString();
// enemyAnim.SetTrigger("isDeadTrigger");
enemyStat.Die();
}
}
}
if (hit.rigidbody != null)
{
hit.rigidbody.AddForce(-hit.normal * impactForce);
}
GameObject impactGO = Instantiate(impactEffect, hit.point, Quaternion.LookRotation(hit.normal));
Destroy(impactGO, 1f);
}
}
}
the enemyHealth script is attached to the enemy prefab and the playerGun script is attached to the palyer prefab.
When I have only one enemy in the scene. it works fine and it kills the enemy and i get some coins. but I bring more types of the enemy prefabs, still works only for one. at the same time the health of all enemies reduces when i shoot at one.
Please help me to find the error. I really don't know what to do.
Answer by xxmariofer · Dec 14, 2020 at 04:21 PM
You cant find the script like this
enemyAnim = GameObject.FindGameObjectWithTag("Enemy").GetComponentInChildren<Animator>();
if you have multiple enemies you will not be able to know which animator find, you should probably rewrite to this
enemyAnim = gameObject.GetComponentInChildren<Animator>();
same goes to the Player Script although is already commented you would need to find the enemy that got hit like you did in this line
enemyStat = hit.transform.GetComponent<EnemyHealth>();
Yes, adding the gameObject worked for me. Thank you very much.
Your answer
Follow this Question
Related Questions
How to make my Player take damage from prefab? 1 Answer
How to make your player health subtract when enemies come into contact with you 3 Answers
Destroy 1 prefab object and changing the color/image 1 Answer
MissingMethodException: UnityEngine.Collision.GetComponent 1 Answer
Prefab shooting damage/health? 1 Answer