Color Lerping continuously with different colours.

IEnumerator ChangeColorAfterTime( float delayTime)
{

		Color currentcolor;
		Color nextcolor;
		while (true) { 
		
			currentcolor = (Color)ColorsList [UnityEngine.Random.Range (0, Colors.Length)];
			nextcolor = (Color)ColorsList [UnityEngine.Random.Range (0, Colors.Length)];    
		
					gameObject.GetComponent<SpriteRenderer> ().color = Color.Lerp (currentcolor, nextcolor, delayTime);

				}

			}
			yield return new WaitForSeconds (delayTime);
		}
	}

I have a list of Colors and what i want is to continuously Lerp the color of gameObject from current to nextColor and i want this to happen in time delayTime , currently what happens is it changes its color from current to next in delay time but it doesn’t Lerp it changes from current to next after the specified time is over

Please, see my answer to this post to understand how Lerp works :

Don’t forget to read the doc too : Unity - Scripting API: Color.Lerp

I haven’t tested the following code, but give it a try :wink:

IEnumerator ChangeColorAfterTime( float delayTime)
{
	SpriteRenderer spriteRenderer = gameObject.GetComponent<SpriteRenderer> () ;
	Color currentcolor = (Color)ColorsList [UnityEngine.Random.Range (0, Colors.Length)];;
	Color nextcolor;
	
	spriteRenderer.color = currentcolor ;
	
	while (true)
	{
		nextcolor = (Color)ColorsList [UnityEngine.Random.Range (0, Colors.Length)];

		for( float t = 0 ; t < delayTime ; t += Time.deltaTime )
		{
			spriteRenderer.color = Color.Lerp (currentcolor, nextcolor, t / delayTime );
			yield return null ;
		}
		currentcolor = nextcolor ;
	}
}