# Opacity of Lightmapped/Bumbed Diffuse Object

Is it possible to fade out an Object with that shader somehow?

I want to create a object that fades away after pushing a button. So the opacity is going from 100 to zero over a certain period of time?

Ok, I am getting somewhere.

I have this code inside of a Update function:

``````function Update () {
var alpha = Mathf.Lerp(maximum, minimum, (Time.time));
var newColor = Color (1.0, 1.0, 1.0, alpha);
print(alpha);

dak1.renderer.material.color = newColor;
if(alpha == minimum) {
dak1.renderer.enabled = false;
}
}
}
}
``````

According to the scripting reference the Mathf.Lerp should fade from/to in one second if used this way. However this is not working for me because it’s at zero in much less then a second. And what makes it one second? Can I have control over that?

Thanks, Hoping for a reaction

EDIT: Hmmm, I dont get the Time function. I just want the variable maximum fade to the variable minimum over a certain period of time?

EDIT: I thought I was getting somewhere but no. This is related to the time since the game started… .

``````	if(changeShader) {
alpha = maximum - Time.deltaTime * timeRate;
//alpha = Mathf.Lerp(maximum, minimum, Time.deltaTime / 1.0);
newColor = Color (1.0, 1.0, 1.0, alpha / 100);
print(alpha / 100);
dak1.renderer.material.color = newColor;
if(alpha <= minimum) {
dak1.renderer.enabled = false;
}
}
``````

Time.deltaTime is the amount of time since the previous frame. If you want to count time since some arbitrary event, you need to create a variable of your own, zero it when your event occurs, and then update it each frame.

The third parameter to Mathf.Lerp() needs to be between 0 (meaning entirely the first value) and 1 (meaning entirely the second value).

So if you want to fade something out over time, you need to do something like this:

``````var fade : float = 0.0;
var rate : float = 3; // fades over 3 seconds

// Fade will start at 0, and grow to 1 over the course of 3 seconds.
function Update() {

// Use fade as an alpha value, or as a parameter to Lerp(), here
}
``````

It’s much simpler to just make a separate function for that instead of trying to work it into Update().

``````function FadeOut () {
for (i = 1.0; i >= 0.0; i -= Time.deltaTime) {
renderer.material.color.a = i;
yield;
}
}
``````

–Eric

Thanks a lot!

I"s fixed it but it was yesterday allready with the help of Omar on the IRC.

The code became this and it works great.

``````//The camera's
var camera1:GameObject;
var camera2:GameObject;
//Parts of the roof
var dak1:GameObject;
var dak2:GameObject;
var dak3:GameObject;

var minimum = 0.0;
var maximum:float = 1.0;

var duration = 0.1;

private var startTime = 0.0;

/** Variables to cache the shaders **/

/** Start is called once at the beggining of the script **/
function Start ()
{

}

//Creer GUI elementen.
function OnGUI () {
if (GUI.Button (Rect (10,10,100,30), "Eerste persoons camera")) {
camera1.camera.enabled = true;
camera2.camera.enabled = false;
}
if (GUI.Button(Rect (120,10,100,30), "Roteer om chalet")) {
camera1.camera.enabled = false;
camera2.camera.enabled = true;
}

if (camera2.camera.enabled == true)
{
if (GUI.Button(Rect (230,10,140,30), "Dak"))
{

startTime = Time.time;
}
}
}

//Verander de materialen naar een shader met transperacy en fade ze weg door de opacity te veranderen gedurende een tijd.
function Update () {

var alpha : float = 0.0;

{
alpha = ((Time.time - startTime) / duration);
dak1.renderer.material.color.a = Mathf.Clamp01 (alpha); /** We clamp to 0-1 to remove unexpected results **/
dak2.renderer.materials[0].color.a = Mathf.Clamp01 (alpha);
dak2.renderer.materials[1].color.a = Mathf.Clamp01 (alpha);
dak3.renderer.material.color.a = Mathf.Clamp01 (alpha);
}
else
{
alpha = ((Time.time - startTime) / duration);
/** here we need the inverse (1.0 - alpha) **/
dak1.renderer.material.color.a = 1.0 - Mathf.Clamp01 (alpha); /** We clamp to 0-1 to remove unexpected results **/
dak2.renderer.materials[0].color.a = 1.0 - Mathf.Clamp01 (alpha);
dak2.renderer.materials[1].color.a = 1.0 - Mathf.Clamp01 (alpha);
dak3.renderer.material.color.a = 1.0 - Mathf.Clamp01 (alpha);
}

/** we assign the shader according to the fade state (if its 1.0 (solid) then use lightmap, else use diffuse) **/
if ( dak1.renderer.material.color.a >= 1.0  )
{
/** and using shaderCurrent we only set the shader when needed to **/
{
}
}
else
{