Hello, I think I made a mistake with my code. I’ve been tinkering with my code and now my enemies won’t activate their death particle when they run out of hitpoints. They appear in my hierarchy, but their animations don’t play. If you can help, please contact me at your earliest convenience.
Here is my Enemy Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy : MonoBehaviour
{
[SerializeField] GameObject deathFX;
[SerializeField] ParticleSystem damageFX;
[SerializeField] int scorePerHit = 5;
[SerializeField] int hitsUntilDeath = 5;
ScoreBoard scoreBoard;
GameObject parentGameObject;
int numOfHits;
void Start()
{
scoreBoard = FindObjectOfType<ScoreBoard>();
parentGameObject = GameObject.FindWithTag("SpawnAtRuntime");
AttachRigidbody();
}
void AttachRigidbody()
{
Rigidbody rb = gameObject.AddComponent<Rigidbody>();
rb.useGravity = false;
}
void OnParticleCollision(GameObject other)
{
ProcessHit();
RegisterHit();
}
void ProcessHit()
{
scoreBoard.IncreaseScore(scorePerHit);
numOfHits = numOfHits + 1;
}
void KillEnemy()
{
GameObject vfx = Instantiate(deathFX, transform.position, Quaternion.identity);
vfx.transform.parent = parentGameObject.transform;
Destroy(gameObject);
}
void RegisterHit()
{
if(numOfHits < hitsUntilDeath)
{
ParticleSystem battleDamage = Instantiate(damageFX, transform.position, Quaternion.identity);
battleDamage.Play();
battleDamage.transform.parent = parentGameObject.transform;
}
else
{
KillEnemy();
}
}
}