Fading color/alpha from 1.0 to 0.0

I have an splatter effect I am trying to implement when an enemy is killed with the following script:

private var splatFadeRate : float = 0.33;
private var splatRunRate : float = 0.5;
private var splatDeathRate : float = 2.75;

private var startColor : Color;
private var endColor : Color;

function Start ()
{
    
    startColor = renderer.material.color;
    endColor = Color (startColor.r, startColor.g, startColor.b, 0.0);
    
    Debug.Log ("[Start]  Alpha = " + startColor.a);

}


function Update ()
{

    if (transform.localScale.z < 1.0)
    {
        transform.localScale.x = Mathf.Lerp (transform.localScale.x, 1.0, Time.time);
        transform.localScale.z = Mathf.Lerp (transform.localScale.z, 1.0, Time.time);
    }
    
    if (transform.localScale.z >= 1.0)
    {
        transform.position.y = Mathf.Lerp (transform.position.y, transform.position.y - 3.0, Time.deltaTime);
        transform.localScale.z = Mathf.Lerp (transform.localScale.z, transform.localScale.z + 1.0, Time.deltaTime * splatRunRate);
        renderer.material.color = Color.Lerp (startColor, endColor, Time.time * splatFadeRate);
        Debug.Log ("[Update]  Alpha = " + renderer.material.color.a);
    }

    Destroy (gameObject, splatDeathRate);
    
}

I invoke it in my enemy’s script as follows:

var splat : GameObject = Instantiate(splatPrefab, Vector3(this.transform.position.x, this.transform.position.y, -2), Quaternion.identity);

The problem is that the splatter effect works the first time it is instantiated (game object in hierarchy or instantiated for first round of enemies), but then never displays again upon subsequent instantiations (2nd, 3rd, etc… rounds of enemies, drag and drop from project view to hierarchy). I’ve verified that the alpha jumps from 1.0 to 0.0, seemingly without executing Color.Lerp. I’ve verified that the splatter prefab to which the splatter effect script is attached does indeed instantiate, but the texture/material will not display.

I have a sneaking suspicion I’m effecting the material’s alpha globally, not locally. Any suggestions on correcting this problem?

You’re lerp’ing by Time.time. The value of Time.time starts at zero and continually increases as long as the the game is running. So, as soon as Time.time * splatFadeRate > 1 [i.e. as soon as the game has been running for more than 1/splatFadeRate seconds], the result of your Color.Lerp() call will be endColor every frame.

What you need to do instead is store Time.time in Start() (say, in a variable named fadeStart), then use the difference since that time in the lerp call [i.e. …Lerp(x,y, (Time.time - fadeStart) * splatFadeRate].

Time.deltaTime * splatFadeRate works as well, but I like your solution better. Sometimes the best thing to do is walk away for a spell. Thanks for your insight, laurie.