How to fade out over time?

Hi to all

I’m trying to make a gameobject to gradually disappear using transparency. I could find out that a nice way to do it, is to set its shader material to ‘Transparent/Cutout/VertexLit’ , and then, if I modify the ‘Alpha cutoff’ property to maximum, the object became invisible(fully transparent, doesn’t matter).My question is, how to do this programatically? I have tried this code, but the object disappear instantly, not gradually,as I want, either the value is big or small(theoreticaly 255 is opaque, 0 transparent).The object is textured.

void Update(){
 renderer.material.SetFloat("_Cutoff",200);
 renderer.material.SetFloat("_Cutoff", 150);
 renderer.material.SetFloat("_Cutoff", 100);
 renderer.material.SetFloat("_Cutoff", 50);
 renderer. material.SetFloat("_Cutoff",0);

}

What you’re currently doing is setting the cutoff to 200, then 150, 100, etc every frame, so at the end of every frame it will be zero - so as you say it’ll be invisible right from the start.

The easiest way to do this is set up a coroutine.

IEnumarator FadeOut( float time, Material material )
{
    float index = 0.0f;
    float rate = 1.0f/time;
    while( index < 1.0f )
    {
        index -= rate;
        material.SetFloat( "_Cutoff", index );
        yield return null;
    }

}

You can then simply call this coroutine to fade it out in a certain amount of time from, for instance, Start

float fadeOutTime = 3.0; 
void Start()
{
    StartCoroutine( FadeOut( fadeOutTime, renderer.material ) );
}