- Home /
alphaKeys in linerenderer gradient not being set
I'm using a gradient on a linerenderer, more specifically it's alpha, to make an effect of moving in and out of the ground. In theory this should work fine, but wherever I attempt to actually SET any kind of variable in the gradient, it doesn't work.
Renderer.colorGradient.alphaKeys[alphaPos].alpha = pos.z;
(where alphaPos is which key to edit (out of 8. There will always be 8 in my circumstance, not that me setting it to have 8 does anything anyways)
Renderer.colorGradient.SetKeys(Renderer.colorGradient.colorKeys, newKeys)
, or Renderer.colorGradient.alphaKeys = newKeys
(where newKeys is an array of the alphaGradientKeys) also do not work.
If I were to print pos.z
and Renderer.colorGradient.alphaKeys[alphaPos].alpha
, pos.z
would be, say .5f, .7f, .100000003f, etc...
the Renderer.colorGradient.alphaKeys[alphaPos].alpha
would remain to be 1
, and never be changed.
Edit: Making a new gradient and assigning it's alphaKeys, and then setting the colorGradient to that new gradient works, but even in that case I cannot manually set newGradient.alpha
. This is troubling as I need this process to be as optimized as possible because this is happening many times per frame.
Answer by TDZ · Mar 27, 2018 at 01:14 PM
GradientAlphaKey[] alphakeys = new GradientAlphaKey[c.alphaKeys.Length];
for (int j = 0; j < c.alphaKeys.Length; j++)
{
alphakeys[j] = new GradientAlphaKey(c.alphaKeys[j].alpha * _alphaPulse, c.alphaKeys[j].time);
}
c.SetKeys(c.colorKeys,alphakeys);
_lineRenderers[i].colorGradient = c;
you just need to generate a brand new gradientalphakey and apply it instead of just changing the value in it.
Don't this this: c.alpha$$anonymous$$eys[j]
. Gradient.alpha$$anonymous$$eys is a property. Reading that property will create a new array every time you read it. You can simply do:
Gradient c = lineRenderer.colorGradient;
GradientAlpha$$anonymous$$ey[] alpha$$anonymous$$eys = c.alpha$$anonymous$$eys;
for (int j = 0; j < alpha$$anonymous$$eys.Length; j++)
{
alpha$$anonymous$$eys[j].alpha = yourValue;
}
c.alpha$$anonymous$$eys = alpha$$anonymous$$eys;
lineRenderer.colorGradient = c;
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
help! I can't get an array from another script 1 Answer
Listing multiple objects 2 Answers
Copy Childen Of GameObjects From Array 1 To Array 2 1 Answer