I’m trying to get this enemy function to wait for the death animation to play before destroying the game object, otherwise it gets destroyed before the animation finishes. I’ve looked online and it seems that I’m doing everything right, but it just won’t wait.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyHealth : MonoBehaviour
{
[SerializeField] float health, maxHealth = 3f; // This creates two variables, health and max health. SerializeField is needed so that these variables can stay private but be accessible within the Unity editor
public Animator anim;
private void Start()
{
health = maxHealth;
}
private IEnumerator Wait()
{
yield return new WaitForSeconds(5);
}
public void TakeDamage(float damageAmount) // Method that takes in a damage amount as a float and inflicts it upon the enemy
{
health -= damageAmount; // The damage amount is taken away from the current health
if (health <= 0) // If the enemy has not more health left once it has taken damage:
{
anim.SetTrigger("Death");
StartCoroutine (Wait());
Debug.Log("Waiting");
Destroy(gameObject); // Then it dies
}
}
}
Thank you so much for the explanation!
The reason that I’m not doing research into coroutines is because this is a school project where I predict that I will only use this feature once, and I only have a few days to finish basically the entire thing cos I’m an idiot
But I will definitely look over this again to make sure I understand it fully once I have the time, so I appreciate this a bunch!