# Why one colorKey in ParticleSystem cannot be changed without a new Gradient?

My question is partly a continuation of this question. So, why I can't change just one GradientColorKey this way (without replacing the whole Gradient instance):

ColorOverLifetimeModule colorOverLifetime = _particle.colorOverLifetime;


colorOverLifetime.color is MinMaxGradient struct, so I copy its value in a variable color.

The colorOverLifetime.color.gradient property is Gradient class and there is no need to copy it.

The colorKeys property is just an array of GradientColorKey struct. So, I assign a new GradientColorKey value to the array element that already exists.

Why shouldn't this work?

Well, while I was writing this question, I think I found the answer along the way. If you look into the implementation of the ColorOverLifetimeModule struct color property it should be clear why my code doesn't work:

  public ParticleSystem.MinMaxGradient color
{
get
{
}
set
{
}
}


in the getter they return a reference to a copy of the MinMaxGradient instance.

So, I tried to fix my code this way:

ColorOverLifetimeModule colorOverLifetime = _particle.colorOverLifetime;


but this still didn't work. I noticed that colorKeys[1] hadn't changed after the assignment. I could find only this piece of implementation of the Gradient class colorKeys[] property:

extern public GradientColorKey[] colorKeys
{
}


so I can't say what's going on in there. Likely, they use the same pattern - return a copy of the array.

If so, it should work this way:

ColorOverLifetimeModule colorOverLifetime = _particle.colorOverLifetime;

Summary: we don't need to explicitly recreate the Gradient, but we need to copy all these properties to variables and then copy them back.