Control the time transition Fading between colors

I am having problems trying to fix this script…

I want to control the time of transition between the colors. But I don’t know why is not working the fading. Now I can change the Color without problems,

Please any advice is more than welcome.

    var startColor : Color;
    var myColors  : Color[];
    var NextColor : int = 0;
    var fadeSpeed : float = 3.5;

    function NextMaterialUsed()

  GetComponent.<Renderer>().material.color = Color.Lerp(startColor,myColors[NextColor],Time.deltaTime * fadeSpeed);
    if(NextColor < myColors.length-1)
        NextColor += 1;
    NextColor = 0;

Hi Paco,

IMHO you should try to have stateless helpers that’s gonna be easyer to debug and maintain. ie something like :

int GetBaseColorIndexFromTime(float time, int numColors, float fadeSpeed)
     return min( (int)time/fadeSpeed, numColors) ;

Color GetColorFromTime(float time, Color[] myColors, float fadeSpeed)
    int srcColorIndex = GetBaseColorIndexFromTime(time, myColors.Length -1, fadeSpeed);
    int destColorIndex = min( srcColorIndex + 1, myColors.Length -1) ;

    float lerpFactor = (time % fadeSpeed) / fadeSpeed;
    return Color.Lerp(myColors[srcColorIndex], myColors[destColorIndex], lerpFactor );

PS: draft code, but should not be too far from actual c#

Hope it helps :slight_smile: