Search Unity

LineRenderer color gradient not updating

Discussion in 'Scripting' started by PenDigi, Nov 4, 2017.

  1. PenDigi

    PenDigi

    Joined:
    Sep 19, 2015
    Posts:
    14
    Hello all, I have an issue with the LineRenderer's color gradient not working. Currently I have a LineRenderer with 3 colorKeys and a plain white material set.

    I calculate the center colorKeys new time and set it in the following function but the line renderer is not updating.

    Code (CSharp):
    1.     void SetInteractionPoint(float weight)
    2.     {
    3.         m_Weight = Mathf.Clamp(weight, 0.0f, 1.0f);
    4.  
    5.         var ck = m_LineRenderer.colorGradient.colorKeys;
    6.         ck[1].time = m_Weight;
    7.         m_LineRenderer.colorGradient.SetKeys(ck, m_LineRenderer.colorGradient.alphaKeys);
    8.  
    9.         Debug.Log(ck[1].time);
    10.     }
     
  2. hallidev

    hallidev

    Joined:
    Jan 21, 2017
    Posts:
    14
    Did you ever find a solution for this? Same thing is happening to me. When I attach the debugger, SetKeys sets the keys, but stepping to the next statement has them reverted. I'm confused...
     
  3. Iron-Warrior

    Iron-Warrior

    Joined:
    Nov 3, 2009
    Posts:
    838
    Also having this same issue. Has anyone found a solution? If not will file a bug report.
     
  4. Struck89

    Struck89

    Joined:
    Jul 28, 2017
    Posts:
    1
    I had this problem, and I guess previous commenters fixed it already.

    I am no expert and I don't know why, but it is needed to generate a new Gradient instead of modifying the old one.


    Gradient grad = new Gradient();
    GradientColorKey startGrad = new GradientColorKey(Color.black, 0);
    GradientColorKey progressGrad0 = new GradientColorKey(Color.black, value-0.1f);
    GradientColorKey progressGrad1 = new GradientColorKey(Color.white, value);
    GradientColorKey progressGrad2 = new GradientColorKey(Color.black, value+0.1f);
    GradientColorKey endGrad = new GradientColorKey(Color.black, 1);
    grad.colorKeys = new GradientColorKey[] { startGrad, progressGrad0, progressGrad1, progressGrad2, endGrad };
    li.colorGradient = grad;


    "li" of course is the LineRenderer component that we want to modify.
     
  5. Jonas-Neuston

    Jonas-Neuston

    Joined:
    Jun 10, 2017
    Posts:
    70
    I'm having the same issue.
     
  6. Bizquit110

    Bizquit110

    Joined:
    Nov 15, 2016
    Posts:
    12
    This code worked for me:
    Code (CSharp):
    1.  private float previousAlpha;
    2.  
    3.         public void ChangeAlpha(float alpha)
    4.         {
    5.             if (Math.Abs(previousAlpha - alpha) > 0.01f)
    6.             {
    7.                 previousAlpha = alpha;
    8.                
    9.                 var copyGradient = LineRenderer.colorGradient;
    10.                 var alphaArray = copyGradient.alphaKeys;
    11.                 alphaArray[1].alpha = alpha;
    12.                 copyGradient.alphaKeys = alphaArray;
    13.            
    14.                 LineRenderer.colorGradient = copyGradient;
    15.             }
    16.         }
     
    Last edited: Jan 7, 2021
  7. StartStart

    StartStart

    Joined:
    Jan 2, 2013
    Posts:
    150
    Thanks, This worked for me too.
     
  8. The_Pied_Shadow

    The_Pied_Shadow

    Joined:
    Jul 25, 2018
    Posts:
    14
    This also worked for me and it's honestly absurd. Every step of this was necessary to get it to work. SetKeys doesn't work. Setting the keys individually doesn't work. Setting them to new structs of alphas keys doesn't work. Yet it lets you do all this. It says they have setting functions but then just ignores the setting. This shouldn't be the only way to make this happen. It's extremely smelly to have to break down every class and struct into completely new versions of it and set it all back. I'm on latest recommended version and this is still the case.
     
    Last edited: Jul 7, 2023
    AdamFoster, a5k9ga6a and GibTreaty like this.
  9. yackson-cc

    yackson-cc

    Joined:
    May 14, 2016
    Posts:
    48