Question by
dpowergames · May 06, 2019 at 07:23 PM ·
shootingenemy healthdieenemy damagehealth
Why will my enemy not die?
In my simple game, I have two enemies. One has a health value of 1, and the other a health value of 2. When a collision is detected a take damage function is called which takes 1 away from the health value. When the health value is <= 0, the enemy dies. This works perfectly fine for the enemy with only a health value of 1, but when the health value is larger than the value of the damage exerted by the bullet, no matter how many times it is shot, the enemy will not die. Could anyone tell me why this might be happening? Thank you in advance.
My code is listed below. I am referencing a damage variable from the bullet script.
public Rigidbody enemyRb;
public float enemySpeed;
public int health;
public GameObject deathEffect;
public Material[] material;
Renderer rend;
public int enemyDamage = 1;
// Start is called before the first frame update
void Start()
{
enemyRb.velocity = transform.forward * -1 * enemySpeed * Time.deltaTime;
rend = GetComponent<Renderer>();
rend.enabled = true;
rend.sharedMaterial = material[0];
}
// Update is called once per frame
void Update()
{
if (gameObject.transform.position.z > 40f) //Defining an activation point where the enemy is now kill-able.
{
health = 999;
rend.sharedMaterial = material[0];
}
else
{
health = 2;
rend.sharedMaterial = material[1];
}
}
public void TakeDamage2(int damage)
{
health -= damage; // I think this is where the error may be occurring. The health might be returning to 2 every time a collision is detected and so health never reaches 0.
Debug.Log("took damage");
if (health <= 0)
{
Die();
Debug.Log("he ded");
Score.scoreValue += 2;
}
}
Comment