Color.Lerp sometimes takes the colors out of order

    public Color color1;
    public Color color2;

void Start()
    {
        color1 = UnityEngine.Random.ColorHSV();
        color2 = UnityEngine.Random.ColorHSV();
    }

void Update()
    {
        float t = Mathf.PingPong(Time.time, duration) / duration;
        mCam.backgroundColor = Color.Lerp(color1, color2, t);
        timesincechange += Time.deltaTime;

        if (timesincechange >= 30 && timesincechange <= 30.1)
        {
            color1 = UnityEngine.Random.ColorHSV();
           
        }
        if (timesincechange >= 60 && timesincechange <= 60.1)
        {
            color2 = UnityEngine.Random.ColorHSV();           
        }
        //safety if, if the program skips the second colorchange
        if (timesincechange > 60)
        {
            timesincechange = 0;
        }
    }

What i expect is to get the color to go from a to b however its 50/50 sometimes it starts from b to a which messes up the visuals.

That’s literally what Mathf.PingPong is designed to do though.

https://docs.unity3d.com/ScriptReference/Mathf.PingPong.html

Maybe instead you should switch to using lerp and reset duration when you start a new lerp. That would probably get you the behavior you’re looking for.

I kind of found the problem, it wasn’t pingpong actually it was that the timer inside of the pingpong doesnt reset itself when there is a restart however now i can’t fix that problem. timer cannot be set to 0

OK NWM my solution was wrong however changing code this sadly didn’t change much. now when it comes to changing color back to color 1(15th row) happens at an instant for some reason.

timer = Time.time;

        //colorwatcher = mCam.backgroundColor;

        t = Time.time / duration ;
        //t = Mathf.PingPong(timer, duration) / duration;
        //mCam.backgroundColor = Color.Lerp(color1, color2, t);
        timesincechange += Time.deltaTime;

        if (timesincechange <30)
        {
            mCam.backgroundColor = color1;
            mCam.backgroundColor = Color.Lerp(color1, color2, t); 
        }
        else if (timesincechange >30)
        {
            mCam.backgroundColor = color2;
            mCam.backgroundColor = Color.Lerp(color2, color1, t);
        }

        if (timesincechange >= 30 && timesincechange <= 30.1)
        {
            color1 = UnityEngine.Random.ColorHSV();

        }
        else if (timesincechange >= 60 && timesincechange <= 60.1)
        {
            color2 = UnityEngine.Random.ColorHSV();
        }
        //safety if, if the program skips the second colorchange
        else if (timesincechange > 60)
        {
            timesincechange = 0;
        }

i fixed it in the cheapest way possible:

    public void colorupdate()
    {
       
        if (colmod == false)
        {
            mCam.backgroundColor = Color.Lerp(color1, color2, t-tMinus);
            if (mCam.backgroundColor == color2)
            {
                color1 = UnityEngine.Random.ColorHSV();
                colmod = true;
                tMinus++;
            }
        }
        else
        {
            mCam.backgroundColor = Color.Lerp(color2, color1, t-tMinus);
            if (mCam.backgroundColor == color1)
            {
                color2 = UnityEngine.Random.ColorHSV();
                colmod = false;
                tMinus++;
            }
        }
    }

After that i just set tMinus to 0 when a new game starts.