Search Unity

Insert a lerp into list

Discussion in 'Scripting' started by VeTK85, Jun 27, 2019.

  1. VeTK85

    VeTK85

    Joined:
    Jun 26, 2016
    Posts:
    54
    Hi, could you give me a direction, I'm trying to set a lerp on the necessary elements of a color list, but nothing I pass does not come out of this error

    Unexpected symbol `_color'


    Code (CSharp):
    1.      public List<SpriteRenderer> sprit = new List<SpriteRenderer>();
    2.      public List<Color> _color = new List<Color>();
    3.      public Color lerpColor;
    4.      public float speed;
    5.      void Start() {
    6.          sprit = GetComponent<SpriteRenderer>()
    7.          _color[6] = Color.red;
    8.          _color[7] = Color.green;
    9.          _color[8] = Color.yellow;
    10.      }
    11.      void Update() {
    12.          sprit.color = Color.Lerp (_color, lerpColor, Mathf.PingPong(Time.time * speed, 1.0f));
    13.      }
     
  2. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,635
    The precise error you're getting is that on line 6 you are missing a semi-colon ';' at the end of the line.
     
  3. VeTK85

    VeTK85

    Joined:
    Jun 26, 2016
    Posts:
    54
    truth had not realized, but more error appears, could you tell me what can be added.
    the first asks to convert to unitysystem, but nothing has effect and the error continues
    Code (CSharp):
    1. public System.Collections.Generic.List<UnityEngine.SpriteRenderer> sprit;
     
  4. xjjon

    xjjon

    Joined:
    Apr 15, 2016
    Posts:
    612
    Well you are trying to set `color` on a list.

    You need to set it on one of the `SpriteRenderer` objects inside the `sprit` list.

    sprit.color should be something like

    Code (CSharp):
    1.  
    2. foreach(SpriteRenderer r in sprit) {
    3.   r.color = Color.Lerp (_color, lerpColor, Mathf.PingPong(Time.time * speed, 1.0f));
    4. }
    Check out: https://docs.unity3d.com/ScriptReference/SpriteRenderer-color.html
     
  5. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,635
    The rest of these errors occur because sprit is a list of SpriteRenderers and you are trying to use it as if it were just a SpriteRenderer.

    For example, where you say sprit.color - a list of SpriteRenderers does not have a color. Each of the SpriteRenderers in the list has color, though.

    In order to access an individual SpriteRenderer in sprit, you have to use an index. sprit[0].color will work, or sprit[1].color. but sprit.color doesn't make any sense. Edit: Or you can use foreach like xjjon said

    Why are you using a list, though? Does your object have more than one SpriteRenderer?
     
  6. VeTK85

    VeTK85

    Joined:
    Jun 26, 2016
    Posts:
    54
    the worst I've done this before.this error follows me
     
  7. VeTK85

    VeTK85

    Joined:
    Jun 26, 2016
    Posts:
    54
    the above error image is with the foreach inserted
    Yes, I use multiple sprites.
    this is weird
     
  8. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,635
    What does your code look like now?
     
  9. WallaceT_MFM

    WallaceT_MFM

    Joined:
    Sep 25, 2017
    Posts:
    394
    An issue I see is that _color is actually a List<Color>, so you can't use it in a Color.Lerp. You could use a gradient instead, otherwise you'll need to write a method for lerping a color list. Also, you never change lerpColor, so this will probably not do what you want. I wrote this float gradient lerp a while ago, feel free to re-purpose it as you see fit.
    Code (CSharp):
    1.         /// <summary>
    2.         /// Samples a linear float gradient with a number of key frames and corresponding values.
    3.         /// The gradient will have a value of values[i] at a position of keys[i], so each key serves as an "index" into the
    4.         /// gradient and the values define the "shape" of the gradient.
    5.         /// </summary>
    6.         /// <param name="keys">An ascending list of keys into the gradient. Behavior is undefined for unsorted key arrays.</param>
    7.         /// <param name="values">A list of values for the gradient at positions specified by the keys array.</param>
    8.         /// <param name="samplePoint">Where to sample the gradient</param>
    9.         /// <returns>the value of the specified gradient at samplePoint</returns>
    10.         public float SampleGradient(float[] keys, float[] values, float samplePoint)
    11.         {
    12.             if (keys == null) throw new System.ArgumentException("keys is null");
    13.             if (values == null) throw new System.ArgumentException("values is null");
    14.             if (keys.Length != values.Length) throw new System.ArgumentException("keys and values have different lengths");
    15.  
    16.             int i = 1;
    17.             for (; i < keys.Length - 1 && keys[i] < samplePoint; i++) ; // Linear search
    18.             float newT = (samplePoint - keys[i - 1]) / (keys[i] - keys[i - 1]); // Change of coordinates from gradient-space to normalized space between the two keys
    19.             return Mathf.LerpUnclamped(values[i - 1], values[i], newT); // Lerp between gradient values
    20.         }
     
  10. xjjon

    xjjon

    Joined:
    Apr 15, 2016
    Posts:
    612
    please post your current code and we can help you better
     
  11. VeTK85

    VeTK85

    Joined:
    Jun 26, 2016
    Posts:
    54
    my script is like this

    Code (CSharp):
    1.     public List<SpriteRenderer> sprit = new List<SpriteRenderer>();
    2.     public List<Color> _color = new List<Color>();
    3.     public Color Transparent;
    4.     public float speed;
    5.  
    6.     void Start() {
    7.     sprit = GetComponent<SpriteRenderer>();
    8.     _color[5] = Color.red;
    9.     _color[6] = Color.green;
    10.     _color[7] = Color.red;
    11.     }
    12.  
    13.     void Update()
    14.     {
    15.     foreach(SpriteRenderer r in sprit)
    16.     {
    17.     r.color = Color.Lerp (_color, Transparent, Mathf.PingPong(Time.time * speed, 1.0f));
    18.     }
    19.     }
    20.  
    21.     //From here down,
    22.     //it works normally
    23.     void Right()
    24.     {
    25.     int Index = _color.IndexOf(sprit[0].color);
    26.  
    27.     if (Index < _color.Count - 1) {
    28.     foreach (SpriteRenderer sprite in sprit)
    29.     {
    30.     sprite.color = _color[Index + 1];
    31.     }
    32.     }
    33.     }
    34.  
    35.     void Left()
    36.     {
    37.     int Index = _color.IndexOf(sprit[0].color);
    38.  
    39.     if (Index > 0)
    40.     {
    41.     foreach (SpriteRenderer sprite in sprit)
    42.     {
    43.     sprite.color = _color[Index - 1];
    44.     }
     
  12. xjjon

    xjjon

    Joined:
    Apr 15, 2016
    Posts:
    612
    Changed a few things from your code:

    • Changed sprit from a List<SpriteRenderer> to just a single SpriteRenderer. Since you are using GetComponent() I assume you want to use the SpriteRenderer on the component. So you don't need a list there.
    • Removed all the foreach loops, you only are updating the one SpriteRenderer so you don't need to loop through them.

    I'm not sure what you wish to do with left/right. But right now on Update() it will Lerp from the current color to the color in Transparent.

    Code (CSharp):
    1. public SpriteRenderer SpriteRenderer;
    2.     public List<Color> _color = new List<Color>();
    3.     public Color Transparent;
    4.     public float speed;
    5.  
    6.     void Start()
    7.     {
    8.         SpriteRenderer = GetComponent<SpriteRenderer>();
    9.         _color[5] = Color.red;
    10.         _color[6] = Color.green;
    11.         _color[7] = Color.red;
    12.     }
    13.  
    14.     void Update()
    15.     {
    16.         SpriteRenderer.color = Color.Lerp(
    17.             SpriteRenderer.color,                     //This is the current color
    18.             Transparent,                             //This is the target color you want to Lerp to
    19.             Mathf.PingPong(Time.time * speed, 1.0f));
    20.     }
    21.  
    22.     //From here down,
    23.     //it works normally
    24.     void Right()
    25.     {
    26.         int index = _color.IndexOf(SpriteRenderer.color);
    27.  
    28.         if (index < _color.Count - 1)
    29.         {
    30.             SpriteRenderer.color = _color[index + 1];  
    31.         }
    32.     }
    33.  
    34.     void Left()
    35.     {
    36.         int index = _color.IndexOf(SpriteRenderer.color);
    37.  
    38.         if (index > 0)
    39.         {
    40.             SpriteRenderer.color = _color[index - 1];
    41.         }
    42.     }
     
  13. VeTK85

    VeTK85

    Joined:
    Jun 26, 2016
    Posts:
    54
    did not work very well.
    because I use multiple sprites I need to use a list or array, so I tried using gameobjectfind but it is not good for optimization, I think for this occasion, lerp also does not work
     
  14. VeTK85

    VeTK85

    Joined:
    Jun 26, 2016
    Posts:
    54
    I would use it, but I do not even know how to use the gradient for this occasion.^^
     
  15. WallaceT_MFM

    WallaceT_MFM

    Joined:
    Sep 25, 2017
    Posts:
    394
    You have to modify it a little
    Code (CSharp):
    1. public Color SampleGradient(float[] keys, Color[] values, float samplePoint)
    2.     {
    3.         if (keys == null) throw new System.ArgumentException("keys is null");
    4.         if (values == null) throw new System.ArgumentException("values is null");
    5.         if (keys.Length != values.Length) throw new System.ArgumentException("keys and values have different lengths");
    6.  
    7.         int i = 1;
    8.         for (; i < keys.Length - 1 && keys[i] < samplePoint; i++) ; // Linear search
    9.         float newT = (samplePoint - keys[i - 1]) / (keys[i] - keys[i - 1]); // Change of coordinates from gradient-space to normalized space between the two keys
    10.         return Color.LerpUnclamped(values[i - 1], values[i], newT); // Lerp between gradient values
    11.     }
    12.  
    13.     /// <summary>
    14.     /// Sample from an array of colors like a gradient with evenly spaced colors.
    15.     /// </summary>
    16.     /// <param name="values">the colors of the gradient</param>
    17.     /// <param name="t">a float between 0 and 1 specifying where to sample the gradient</param>
    18.     /// <returns></returns>
    19.     public Color SampleGradient(Color[] values, float t)
    20.     {
    21.         if (values == null) throw new System.ArgumentException("values is null");
    22.  
    23.         float scaledT = (t * (values.Length - 1));
    24.         int lower = Mathf.Clamp((int)scaledT, 0, values.Length - 2);
    25.         return Color.LerpUnclamped(values[lower], values[lower + 1], scaledT - lower);
    26.     }
    Then you can call it like this
    Code (CSharp):
    1. SpriteRenderer.color = SampleGradient(_color, Mathf.PingPong(Time.time * speed, 1.0f));
     
    Last edited: Jun 28, 2019