Ping pong not working

I’m trying to insert a ping pong lerp between one color and another but nothing happens, what’s missing

    public Color colorOne;
    public Color Alpha;

    void Update(){
        Alpha = Color.Lerp(colorOne, Alpha, Mathf.PingPong(Time.time, 1));
    }

You’re taking a lerp between colorOne and Alpha and assigning it to Alpha. This will immediately set Alpha to the same value as colorOne (since Time.time will be near 0 the first time it runs), at which point it will be pingponging between two copies of the same color.

Assign the result of the lerp to a third variable instead of using one of the two you’re feeding into it.

try :

public Color colorOne;
public Color colorTwo;
public Color Alpha;
    void Update(){
        Alpha = Color.Lerp(colorOne, colorTwo, Mathf.PingPong(Time.time, 1));
    }

It’s very strange this, he does ping pong in the inspector, but he is not attacking the sprite.

    public List<SpriteRenderer> list;
    public Color colorOne,color;
    public Color Alpha;


    void Update()
    {
        for (int i = 0; i < List.Count; i++)
        {
            list[i].GetComponent<SpriteRenderer>().color = colorOne;
            colorOne = Color.Lerp(Alpha, color, Mathf.PingPong(Time.time, 1));
        }
    }

The GetComponent call on line 10 is unnecessary (You already have the SpriteRenderer). You should also move line 11 before the for loop, as that result won’t change as you go through the loop.
All that said, that code ought to work. Have you linked the appropriate sprite object in your “list” in the inspector?

very good, thank you, but one thing left me worried, I use many (for) and keep updating several. This is bad, could this (Crash) in Unity?

    void Update(){
        if(index == 0)
        {
            for (int i = 0; i < List.Count; i++)
            {
                corOne = Color.Lerp(Alpha, color, Mathf.PingPong(Time.time, speed));
                List[i].color = corOne;
            }
        }
        else if (index == 1)
        {
            for (int i = 0; i < List.Count; i++)
            {
                corOTwo = Color.Lerp(Alpha, color, Mathf.PingPong(Time.time, speed));
                List[i].color = corOTwo;
            }
        }
        else if (index == 2)
        {
            for (int i = 0; i < List.Count; i++)
            {
                corOThree = Color.Lerp(Alpha, color, Mathf.PingPong(Time.time, speed));
                List[i].color = corOThree;
            }
        }

It’s not going to crash Unity by a long shot, no. You’d have to work much harder to crash Unity. :slight_smile:

2 Likes

Thank you for your help:)