- Home /
Set enemy death particle to the colour of the enemy itself
Hey Guys, Im currently stuck on a small issue where i have a bunch of enemy objects that rush towards the player character and try to attack the player. The player is able to shoot and kill the attacking enemies.
Currently ive got my enemies spawning with random colours, though the prefab has a preset material set to it. Code for enemy spawning is below:
//Spawn the enemy at the center of the plane
Enemy spawnedEnemy = Instantiate (enemy, spawnTile.position + Vector3.up, Quaternion.identity) as Enemy;
//Spawn and enemy with a random colour
spawnedEnemy.GetComponent<MeshRenderer>().material.color = new Color(Random.Range (0f, 1f), Random.Range (0f, 1f), Random.Range (0f, 1f), Random.Range (0f, 1f));
So for example if i start the game one of the enemies could be blue and the other could be green.
Ive currently got a particle system in place that activates on the death of the enemy. So once the enemy dies it blows up into small cubes.
I was just wondering how i can change the colour of the explosion for each enemy based on the random colour that theyre instantiated with.
For example if i killed a blue enemy then the partical system would emit blue cubes, if the enemy was green then the particle system would emit green cubes.
Thanks!
Answer by hfiani · Aug 20, 2017 at 03:22 PM
try to give the material in renderer in particle system the same material as the original one.
if not try this:
spawnedEnemy.GetComponent<ParticleSystemRenderer> ().material.color = spawnedEnemy.GetComponent<SpriteRenderer> ().material.color;
Sorry I dont think you understood my question. i WANT all my enemies to have random colours and they currently are spawning with random colours.
I basically want the partical explosion matterial to change to the current colour of the enemy thats been killed.
my answer still applies. i understood 100% your question. this:
spawnedEnemy.GetComponent<ParticleSystemRenderer> ().material.color
means i assumed that the particle system is attached to the enemy himself, is this is not the case, replace spawnedEnemy qith the particle object, like this:
particleObject.GetComponent<ParticleSystemRenderer> ().material.color = spawnedEnemy.GetComponent<SpriteRenderer> ().material.color;
that doesnt really seem to work. This is the code i have so far:
if (damage >= health) {
Destroy(Instantiate(deathEffect.gameObject, hitPoint, Quaternion.FromToRotation(Vector3.forward, hitDirection)) as GameObject, deathEffect.startLifetime);
deathEffect.GetComponent<ParticleSystemRenderer> ().material = transform.GetComponent<$$anonymous$$eshRenderer> ().material;
}
which is in the enemy script as the Particle System Prefab is attached as a component to the enemy prefab
your code is not understandable actually, you are instantiating an object and destroying it in one instruction. AFTER that you are changing the color of the ORIGINAL particle object, and not the one you created.
I don't know if this will work for you but try changing the color (not the whole material in this case) before the destroy(instantiate(...))
if (damage >= health) {
deathEffect.GetComponent<ParticleSystemRenderer> ().material.color = transform.GetComponent<$$anonymous$$eshRenderer> ().material.color;
Destroy(Instantiate(deathEffect.gameObject, hitPoint, Quaternion.FromToRotation(Vector3.forward, hitDirection)) as GameObject, deathEffect.startLifetime);
}
But i will do this:
if (damage >= health) {
GameObject deathEffectTemp = Instantiate(deathEffect.gameObject, hitPoint, Quaternion.FromToRotation(Vector3.forward, hitDirection)) as GameObject;
deathEffectTemp.GetComponent<ParticleSystemRenderer> ().material.color = transform.GetComponent<$$anonymous$$eshRenderer> ().material.color;
Destroy(deathEffectTemp, deathEffect.startLifetime);
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612133134im_/https://answers.unity.com/themes/thub/images/avi.jpg)