Color Lerping not working correctly.

http://hastebin.com/xasenuboju.avrasm

Here’s my code, all I want is for the text mesh to fade to black on input, it fade’s out but to grey, not black, I don’t understand it? I even threw in a checker variable isFading. I’ve rewritten this a few times and every time it fades to grey, not black.

You can’t really compare colors in that way, the color may never reach exactly == the comparison color due to float presicions

Can the shader go to black? Of if you set the materials color property to black, does it actually go black.

Color.Lerp, like other lerp functions, simply interpolates from a source value to a target value given a parameter t between 0.0 - 1.0 so that given 0.0 it will return the first value, given 1.0 it will return the second value, given 0.5 it will return their average, etc.

With that in mind, take a look at this modified script:

//Define variables.
var fontColor : Color;
var fadeColor : Color;
var sound : AudioSource;
    
var delay : float;
    
var text : String[];
var textMesh : TextMesh;
var fadeInDuration : float;
var fadeOutDuration : float;
var isFadingOut : boolean = false;

private var _currentT : float = 0.0f;

function Start()
{
    //Use colors from inspector
    /*fontColor = Color.white;
    fadeColor = Color.black;*/
    
    if(isFadingOut)
    {
        textMesh.renderer.material.color = fontColor;
        _currentT = 1.0f;
    }
    else
    {
        textMesh.renderer.material.color = fadeColor;
        _currentT = 0.0f;
    }
}

function Update ()
{
    if(isFadingOut  _currentT > 0.0f)
    {
        _currentT -= Time.deltaTime / fadeOutDuration;
        if(_currentT < 0.0f)
        {
            _currentT = 0.0f;
        }
        
        textMesh.renderer.material.color = Color.Lerp(fadeColor, fontColor, _currentT);
    }
    else if(!isFadingOut  _currentT < 1.0f)
    {
        _currentT += Time.deltaTime / fadeInDuration;
        if(_currentT > 1.0f)
        {
            _currentT = 1.0f;
        }
        
        textMesh.text = text[0];        
        textMesh.renderer.material.color = Color.Lerp(fadeColor, fontColor, _currentT);
    }
    
    if(Input.GetButtonDown("Start"))
    {
        isFadingOut = true;
        StartGame();
    }
}

function StartGame()
{
    sound.Play();
    yield WaitForSeconds(delay);
    Application.LoadLevel("Home");
}

Notice that we don’t pass along the current material’s color for the lerp operation, but rather the “beginning” and “end” (or faded out and faded in, in your context) colors. We also use a value t that will always be in the 0.0-1.0 range, modified each frame according to our fade times, and check this value to know if we are done lerping.

Use Fade, it makes things a lot easier.

–Eric

Even when the color is set black it goes grey hpjohn. There got to be a simpler way to do it, and I’ve done it this way before, I’ve just never encountered this error before.

Then you need to use/write a different shader, because the current shader is doing something different with the Color property (like not multiplying)

I am not using any shaders.

No, you are, you have to be in order for triangles to be drawn on the screen. The material references a shader.