Change material in Renderer inside Particle System

Hi guys,
As the title I would like to change the material in Renderer at runtime via script.
What I’m trying to do is:

ParticleSystem tmp = Instantiate(splatEffect, transform.position, Quaternion.identity);
var rend = tmp.GetComponent<ParticleSystemRenderer>();
rend.material = gameObject.GetComponent<MeshRenderer>().material;

But no material is set. Am I doing something wrong?

1 Like

Ok my stupid mistake was that i had an effect inside another so the solution and first access the child:

ParticleSystem tmp = Instantiate(splatEffect, transform.position, Quaternion.identity);
ParticleSystem newTmp = tmp.transform.GetChild(0).GetComponent<ParticleSystem>();
var rend = newTmp.GetComponent<ParticleSystemRenderer>();
rend.material = gameObject.GetComponent<MeshRenderer>().material;
1 Like

Also - always use sharedMaterial rather than material where possible

3 Likes

Oh yes, I hadn’t thought of it because of the rush.
Thanks for the tip!

1 Like

how can I change in particle system “trailMaterial” not like “.sharedMaterial” but “.material”?

found: _particleSystemRenderer.materials[1]

correct, but we also have exactly what you guessed it should be: https://docs.unity3d.com/ScriptReference/ParticleSystemRenderer-trailMaterial.html