Question by
unity_246527 · Feb 10 at 06:31 PM ·
enemy
Need help destroying my enemy when he is killed
I have a working enemy that moves to the player and can be killed but when he is killed he still follows me around so I came up with the idea to have him be destroyed but after looking around the internet for a while I have not been able to find any answers or lines of code that work. I am very new to unity and writing code. So I need some help With my problem
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy : MonoBehaviour
{
public Animator animator;
public int maxHealth = 100;
int currentHealth;
// Start is called before the first frame update
void Start()
{
currentHealth = maxHealth;
}
public void TakeDamage(int damage)
{
currentHealth -= damage;
animator.SetTrigger("Hurt");
if(currentHealth <= 0)
{
Die();
}
}
void Die()
{
Debug.Log("Enemy was killed");
animator.SetBool("IsDead", true);
GetComponent<Collider2D>().enabled = false;
this.enabled = false;
}
}
Comment
Your answer
Follow this Question
Related Questions
Time delay enemy respawn 3 Answers
Why is my player dying when defeating an enemy? 1 Answer
My enemy ai won't stay on the ground help please 0 Answers
Increasing Enemy Speed 4 Answers