Enemies won't activate DeathFX

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();

    }

}

}

You’ve either got to have Play on Awake (on your particle effect game object) or you have to use GetComponent<ParticleEffect>().Play(); in your KillEnemy code.

Thank you, I’ll test out the solution when I get the chance

You were right with putting it on Play On Awake, and I apologize for the late response. Thank you for the advice.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms