Emit Particle upon Death

Good Afternoon fellow Devs,

I’m currently working on a Particle System in co-operation with the Ai that emit’s a particle upon the enemy’s death but just before the body is removed and re-spawned.

I’ve went ahead and looked for some tips online, however the issue am having here is that the references to gameobject.GetComponent in Unity 5 has changed and suggests I upgrade thus breaking the script.

Is there a better method to do this? Any tips or suggestions are extremely helpful.

My goal for this code is to perform the following functions in C#

  1. Player kills AI
  2. AI dies
  3. A particle emits around the AI upon death
  4. The particle stops
  5. AI’s body is removed from the game
  6. Re spawns.

I’ve got all except number 3 and 4 working, this is where I am having some trouble.

Code:

public class BloodBath : MonoBehaviour
	{
		//Name of Particle to emit
		ParticleEmitter pe;

		private bool isDead = false;
		public float respawnTime = 10.0f;

		void Start() {
			ParticleEmitter pe = gameObject.GetComponent<ParticleEmitter> ().particleEmitter;
	}
		
		
		void Update() {
			
			if (isDead)
			{
			
				//Play Particle upon Death
				pe.emit = true;
				
				respawnTime -= Time.deltaTime;
				
				if (respawnTime <= 0.0f)
				{
					RemoveBody();
				}
			}
		}
		
		public void AIDead()
		{
			isDead = true;
		}
		
		void RemoveBody() {
			
			Destroy(gameObject);
		}
		
	}
	
}

A good way to do this is to create an empty GameObject, attach the particle emitter to it, and store it as a prefab in your assets folder.

If you make a ‘public GameObject prefabPe’ in your current script in place of the ‘ParticleEmitter pe’, you can then move the prefab from your assets folder onto this script in the unity window.

You call instantiate on that asset like ‘pe = Instantiate(prefabPe, gameObject.transform.postition, gameObject.transform.rotation);’

Then call ‘Destroy(prefabPe, timeToWait);’
Simple Enough Huh? Good Luck!

Ps. you do not need that getComponent with this method, so that can all go.

About the particles

This might help you with the re-construction of the code.