Matching explosion particle colour to an object hit

I want to match the explosion particles colour to the colour of an object hit (or, in my specific instance, to the colour of the root object of the child hit).

I try this but it doesn’t work:

var instantiatedExplosion : GameObject = Instantiate (impactEffect, contact.point,Quaternion.identity);
instantiatedExplosion.GetComponent(ParticleRenderer).materials[0].color = collision.gameObject.transform.root.renderer.material.color;
(where impactEffect is my explosion prefab)

and if I change it to

instantiatedExplosion.GetComponent(ParticleRenderer).material.color = collision.gameObject.transform.root.renderer.material.color;

it sort of works, in that I get the right colour of particles, but the shader I’m using (which is “Particles/Alpha Blended”) is destroyed and I just get particle billboards (but of the right colour).

The following, however, does work but it’s hardly elegant or economical:

var col = rrr.transform.root.renderer.material.color;
var modifiedColors : Color[ ] = instantiatedExplosion.GetComponent(ParticleAnimator).colorAnimation;
modifiedColors[0] = col;
modifiedColors[1] = col;
modifiedColors[2] = col;
modifiedColors[3] = col;
modifiedColors[4] = col;
instantiatedExplosion.GetComponent(ParticleAnimator).colorAnimation = modifiedColors;

Is there a neater way, —maybe a simple error I’m making in the first couple of lines above? THX.

You can use use Material.SetColor with “_TintColor” as the property name. Using Material.color is equivalent to using SetColor with “_Color” as the property name, which Particles/Alpha Blended doesn’t have. In fact it doesn’t seem like your second example should have worked at all.

There’s nothing wrong with your working code, though. For the sake of convenience, you can turn it into a function if you use it in more than one place. It’d say it’s preferable actually, since it doesn’t create a new instance of the material in the scene, unlike Material.SetColor.

–Eric

Many thanks, Eric.