Insert a lerp into list

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’

     public List<SpriteRenderer> sprit = new List<SpriteRenderer>();
     public List<Color> _color = new List<Color>();
     public Color lerpColor;
     public float speed;
     void Start() {
         sprit = GetComponent<SpriteRenderer>()
         _color[6] = Color.red;
         _color[7] = Color.green;
         _color[8] = Color.yellow;
     }
     void Update() {
         sprit.color = Color.Lerp (_color, lerpColor, Mathf.PingPong(Time.time * speed, 1.0f));
     }

The precise error you’re getting is that on line 6 you are missing a semi-colon ‘;’ at the end of the line.

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

public System.Collections.Generic.List<UnityEngine.SpriteRenderer> sprit;

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

foreach(SpriteRenderer r in sprit) {
  r.color = Color.Lerp (_color, lerpColor, Mathf.PingPong(Time.time * speed, 1.0f));
}

Check out: Unity - Scripting API: SpriteRenderer.color

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?

the worst I’ve done this before.this error follows me

the above error image is with the foreach inserted
Yes, I use multiple sprites.
this is weird

What does your code look like now?

An issue I see is that _color is actually a List, 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.

        /// <summary>
        /// Samples a linear float gradient with a number of key frames and corresponding values.
        /// The gradient will have a value of values[i] at a position of keys[i], so each key serves as an "index" into the
        /// gradient and the values define the "shape" of the gradient.
        /// </summary>
        /// <param name="keys">An ascending list of keys into the gradient. Behavior is undefined for unsorted key arrays.</param>
        /// <param name="values">A list of values for the gradient at positions specified by the keys array.</param>
        /// <param name="samplePoint">Where to sample the gradient</param>
        /// <returns>the value of the specified gradient at samplePoint</returns>
        public float SampleGradient(float[] keys, float[] values, float samplePoint)
        {
            if (keys == null) throw new System.ArgumentException("keys is null");
            if (values == null) throw new System.ArgumentException("values is null");
            if (keys.Length != values.Length) throw new System.ArgumentException("keys and values have different lengths");

            int i = 1;
            for (; i < keys.Length - 1 && keys[i] < samplePoint; i++) ; // Linear search
            float newT = (samplePoint - keys[i - 1]) / (keys[i] - keys[i - 1]); // Change of coordinates from gradient-space to normalized space between the two keys
            return Mathf.LerpUnclamped(values[i - 1], values[i], newT); // Lerp between gradient values
        }

please post your current code and we can help you better

my script is like this

    public List<SpriteRenderer> sprit = new List<SpriteRenderer>();
    public List<Color> _color = new List<Color>();
    public Color Transparent;
    public float speed;

    void Start() {
    sprit = GetComponent<SpriteRenderer>();
    _color[5] = Color.red;
    _color[6] = Color.green;
    _color[7] = Color.red;
    }

    void Update()
    {
    foreach(SpriteRenderer r in sprit)
    {
    r.color = Color.Lerp (_color, Transparent, Mathf.PingPong(Time.time * speed, 1.0f));
    }
    }

    //From here down,
    //it works normally
    void Right()
    {
    int Index = _color.IndexOf(sprit[0].color);

    if (Index < _color.Count - 1) {
    foreach (SpriteRenderer sprite in sprit)
    {
    sprite.color = _color[Index + 1];
    }
    }
    }

    void Left()
    {
    int Index = _color.IndexOf(sprit[0].color);

    if (Index > 0)
    {
    foreach (SpriteRenderer sprite in sprit)
    {
    sprite.color = _color[Index - 1];
    }

Changed a few things from your code:

  • Changed sprit from a List 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.

public SpriteRenderer SpriteRenderer;
    public List<Color> _color = new List<Color>();
    public Color Transparent;
    public float speed;

    void Start()
    {
        SpriteRenderer = GetComponent<SpriteRenderer>();
        _color[5] = Color.red;
        _color[6] = Color.green;
        _color[7] = Color.red;
    }

    void Update()
    {
        SpriteRenderer.color = Color.Lerp(
            SpriteRenderer.color,                     //This is the current color
            Transparent,                             //This is the target color you want to Lerp to
            Mathf.PingPong(Time.time * speed, 1.0f));
    }

    //From here down,
    //it works normally
    void Right()
    {
        int index = _color.IndexOf(SpriteRenderer.color);

        if (index < _color.Count - 1)
        {
            SpriteRenderer.color = _color[index + 1];   
        }
    }

    void Left()
    {
        int index = _color.IndexOf(SpriteRenderer.color);

        if (index > 0)
        {
            SpriteRenderer.color = _color[index - 1];
        }
    }

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

I would use it, but I do not even know how to use the gradient for this occasion.^^

You have to modify it a little

public Color SampleGradient(float[] keys, Color[] values, float samplePoint)
    {
        if (keys == null) throw new System.ArgumentException("keys is null");
        if (values == null) throw new System.ArgumentException("values is null");
        if (keys.Length != values.Length) throw new System.ArgumentException("keys and values have different lengths");

        int i = 1;
        for (; i < keys.Length - 1 && keys[i] < samplePoint; i++) ; // Linear search
        float newT = (samplePoint - keys[i - 1]) / (keys[i] - keys[i - 1]); // Change of coordinates from gradient-space to normalized space between the two keys
        return Color.LerpUnclamped(values[i - 1], values[i], newT); // Lerp between gradient values
    }

    /// <summary>
    /// Sample from an array of colors like a gradient with evenly spaced colors.
    /// </summary>
    /// <param name="values">the colors of the gradient</param>
    /// <param name="t">a float between 0 and 1 specifying where to sample the gradient</param>
    /// <returns></returns>
    public Color SampleGradient(Color[] values, float t)
    {
        if (values == null) throw new System.ArgumentException("values is null");

        float scaledT = (t * (values.Length - 1));
        int lower = Mathf.Clamp((int)scaledT, 0, values.Length - 2);
        return Color.LerpUnclamped(values[lower], values[lower + 1], scaledT - lower);
    }

Then you can call it like thisSpriteRenderer.color = SampleGradient(_color, Mathf.PingPong(Time.time * speed, 1.0f));