I’ve been trying to adapt an existing script that allowed GUI Objects to fade out (and then toggle on later). I’m trying to make it work with an GameObject. Not having the best of luck…
//FadeIn Script for Unity - Zerofractal 2006
var buttonName = "Toggle Material";
var fadeDuration:float=0.5;
var initialDelay:float=5;
private var timeLeft:float=0.5;
function Awake () {
timeLeft = fadeDuration;
}
function Update () {
if (initialDelay > 0){
initialDelay = initialDelay-Time.deltaTime;
} else {
if (Input.GetButton (buttonName))
fade(true);
else
fade(false);
}
}
function fade(direction:boolean){
var alpha;
if (direction){
if (renderer.material.color.a < 0.5){
timeLeft = timeLeft - Time.deltaTime;
alpha = (timeLeft/fadeDuration);
renderer.material.color.a=0.5-(alpha/2);
} else {
timeLeft = fadeDuration;
}
} else {
if (renderer.material.color.a > 0){
timeLeft = timeLeft - Time.deltaTime;
alpha = (timeLeft/fadeDuration);
renderer.material.color.a=alpha/2;
} else {
timeLeft = fadeDuration;
}
}
}
Where have I got that wrong?