# Unity - I can't change the “colorOverLifetime” property of the “Particle System” via script

The goal is to remove the particles via script with fade effect. But I'm not just interested in an alternative, I want to understand why my code doesn't work.

The code below is modified to show only the important parts. I can't know before what time the particles will be dissolved, so I can't set the parameter from the editor.

StarsConfig.Stars.colorOverLifetime.color.gradient.SetKeys This is the important point (see the log image). The gradient does not take my new values but alphaKey is correct.

Why ? Thanks

private bool FadeStarts = false;

void Update()
{
}

{

Debug.Log("OFF " + c.color.gradient.alphaKeys[0].alpha + " --> " + g.alphaKeys[0].alpha);

alphaKey[0].alpha -= StarsConfig.VelocityAlpha * Time.deltaTime;
alphaKey[1].alpha -= StarsConfig.VelocityAlpha * Time.deltaTime;

/*

*/

yield return 0;
}

StarsConfig.Stars.Stop(true, ParticleSystemStopBehavior.StopEmittingAndClear);

}


UPDATE

Ed_Marty's suggestion is correct, and the solution is there (thanks!). But I would like to understand what is the difference between these two codes.

This code works

var col = StarsConfig.Stars.colorOverLifetime;

yield return 0;
}


And this doesn't work

while (StarsConfig.Stars.colorOverLifetime.color.gradient.alphaKeys[0].alpha > 0f) {

yield return 0;
}

• Have you tried doing it how the docs recommend? docs.unity3d.com/ScriptReference/… – Ed Marty May 26 '19 at 14:55
• Yes, if you check my code I created a Gradient g but I don't use it, because it has remained from the previous tests. – Baro May 26 '19 at 14:59
• I mean specifically how they fetch the component and assign the gradient and color separately. You’re doing it all in one line. – Ed Marty May 26 '19 at 15:21
• @EdMarty Thank you very much, you helped me to solve, if now someone explains to me the difference between the two codes I would be grateful :) – Baro May 26 '19 at 16:25

ColorOverLifetimeModule.color is a property getter that returns a copy of the MinMaxGradient struct's values.
So your first earlier code StarsConfig.Stars.colorOverLifetime.color.gradient.SetKeys(...) takes this copy, and modified the copy... then discards it. Since you were modifying the copy, your particle system's original values remain unchanged.
The magic line in the new code is col.color = grad, which takes the modified gradient you've made and hands it back to the module to update its internal state.
• How does it hand back? ColorOverLifetimeModule is a struct as well, so we copy it to the col and that's ok, but we never put it back to the StarsConfig.Stars.colorOverLifetime and it still works. Why? – Sergey Nov 22 '20 at 19:20
• Why might this code not work? ................ ColorOverLifetimeModule colorOverLifetime = _particle.colorOverLifetime; MinMaxGradient color = colorOverLifetime.color; color.gradient.colorKeys[1] = new GradientColorKey(new Color(1, 0, 0), 0.5f); colorOverLifetime.color = color; – Sergey Nov 22 '20 at 20:04