- Home /
enemy not disappearing even after health is zero please help fast
using UnityEngine;
public class Health : MonoBehaviour
{
[SerializeField] private float startingHealth;
public float currentHealth { get; private set; }
private Animator anim;
private bool dead;
private enemydestrucatble des;
private void Awake()
{
currentHealth = startingHealth;
anim = GetComponent<Animator>();
des = GetComponent<enemydestrucatble>();
}
public void TakeDamage(float _damage)
{
currentHealth = Mathf.Clamp(currentHealth - _damage, 0, startingHealth);
if (currentHealth > 0)
{
anim.SetTrigger("hurt");
//iframes
}
else
{
if (!dead)
{
anim.SetTrigger("die");
GetComponent<PlayerMovement>().enabled = false;
dead = true;
if (Collision.gameObject.GetComponent<enemydestrucatble>() != null)
{
Destroy(collision.gameObject);
Destroy(gameObject);
}
}
}
}
public void AddHealth(float _value)
{
currentHealth = Mathf.Clamp(currentHealth + _value, 0, startingHealth);
}
}
//enemyscript
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class enemydestrucatble : MonoBehaviour
{
[SerializeField] private float movementDistance;
[SerializeField] private float speed;
[SerializeField] private float damage;
private bool movingLeft;
private float leftEdge;
private float rightEdge;
private Animator ANIM;
private void Awake()
{
leftEdge = transform.position.x - movementDistance;
rightEdge = transform.position.x + movementDistance;
ANIM = GetComponent<Animator>();
}
private void Update()
{
if (movingLeft)
{
if (transform.position.x > leftEdge)
{
transform.position = new Vector3(transform.position.x - speed * Time.deltaTime, transform.position.y, transform.position.z);
}
else
movingLeft = false;
}
else
{
if (transform.position.x < rightEdge)
{
transform.position = new Vector3(transform.position.x + speed * Time.deltaTime, transform.position.y, transform.position.z);
}
else
movingLeft = true;
}
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.tag == "Player")
{
collision.GetComponent<Health>().TakeDamage(damage);
if (collision.gameObject.GetComponent<enemydestrucatble>() != null)
{
Destroy(collision.gameObject);
Destroy(gameObject);
}
}
//projectile script
}
}
using UnityEngine;
public class Projectile : MonoBehaviour
{
[SerializeField] private float speed;
private float direction;
private bool hit;
private float lifetime;
[SerializeField] private float damage;
private Animator anim;
private BoxCollider2D boxCollider;
private void Awake()
{
anim = GetComponent<Animator>();
boxCollider = GetComponent<BoxCollider2D>();
}
private void Update()
{
if (hit) return;
float movementSpeed = speed * Time.deltaTime * direction;
transform.Translate(movementSpeed, 0, 0);
lifetime += Time.deltaTime;
if (lifetime > 5) gameObject.SetActive(false);
}
private void OnTriggerEnter2D(Collider2D collision)
{
hit = true;
boxCollider.enabled = false;
anim.SetTrigger("explode");
if (collision.tag == "Enemy")
{
if(collision.gameObject.GetComponent<enemydestrucatble>() != null)
{
Destroy(collision.gameObject);
Destroy(gameObject);
}
}
}
public void SetDirection(float _direction)
{
lifetime = 0;
direction = _direction;
gameObject.SetActive(true);
hit = false;
boxCollider.enabled = true;
float localScaleX = transform.localScale.x;
if (Mathf.Sign(localScaleX) != _direction)
localScaleX = -localScaleX;
transform.localScale = new Vector3(localScaleX, transform.localScale.y, transform.localScale.z);
}
private void Deactivate()
{
gameObject.SetActive(false);
}
}
, using UnityEngine;
public class Health : MonoBehaviour
{
[SerializeField] private float startingHealth;
public float currentHealth { get; private set; }
private Animator anim;
private bool dead;
private enemydestrucatble des;
private void Awake()
{
currentHealth = startingHealth;
anim = GetComponent<Animator>();
des = GetComponent<enemydestrucatble>();
}
public void TakeDamage(float _damage)
{
currentHealth = Mathf.Clamp(currentHealth - _damage, 0, startingHealth);
if (currentHealth > 0)
{
anim.SetTrigger("hurt");
//iframes
}
else
{
if (!dead)
{
anim.SetTrigger("die");
GetComponent<PlayerMovement>().enabled = false;
dead = true;
if (Collision.gameObject.GetComponent<enemydestrucatble>() != null)
{
Destroy(collision.gameObject);
Destroy(gameObject);
}
}
}
}
public void AddHealth(float _value)
{
currentHealth = Mathf.Clamp(currentHealth + _value, 0, startingHealth);
}
}
try debug logs, check to make sure are destroying the right objects, the are dead, their health below 0, check everything!
Answer by Tinglee82 · Jun 20, 2021 at 04:26 PM
Just to confirm what you're expecting out of your code. You're expecting your enemy to disappear ONLY when either one of the 2 situations happen - (1) when player collides with enemy, or (2) when projectile collides into enemy (because I don't see TakeDamage being applied to enemy).
If above if accurate, I'd suggest your try using either the Debugger or Debug log to confirm that the "collider" argument passed in to your OnTriggerEnter2D methods actually belong to the enemy, just to rule out some silly situations.
Your answer
Follow this Question
Related Questions
Enable script (false) 0 Answers
Why does killing one enemy kill them all? 1 Answer
how to destroy game objects with a raycast and health? 1 Answer
How do i Kill Enemy with Vehicle?? 2 Answers
My wave system isn't working 0 Answers