Fading an Object's Opacity over time via script.

I am using

var colorStart = Color.red;

var colorEnd = Color.green; var duration = 1.0;

function Update () { var lerp = Mathf.PingPong (Time.time, duration) / duration; renderer.material.color = Color.Lerp (colorStart, colorEnd, lerp); }

from the refrence which works great for changing the color. But how can I get it to also change the opacity of the object. I want to fade it away. How do I effect the alpha channel. I have been trying something like this

var colorAlpha = Color.a;

function Awake(){ colorAlpha = 0; }

but to no effect.

Most shaders do not alpha blend. Use a shader from the "Transparent" section.

Edit: The shader you're using doesn't work, because the alpha of the color is not preserved when using lighting. Fortunately, you don't need lighting at all. Here's a shader that will work:

Shader "Tinted Alpha Blend" { 

Properties {
    _Color ("Tint (A = Opacity)", Color) = (1,1,1,1) 
    _MainTex ("Texture (A = Transparency)", 2D) = "" 

SubShader {
    Tags {Queue = Transparent}
    ZWrite Off
    Blend SrcAlpha OneMinusSrcAlpha

    Pass { 
        SetTexture[_MainTex] {
            ConstantColor [_Color]
            Combine texture * constant


If the script is attached to the object you want to fade then this should work to access the alpha value

renderer.material.color.a = 0.5;

Perhaps this shader may work or at least some of the things discussed in the thread


That may have worked in some version of Unity, but in 4.3, I get a compile-time error (Cannot modify a value type return value of `UnityEngine.Material.color’).