- Home /
How to make a GUITexture fade In?
I'm attempting to have a script fade in the alpha of a GUITexture from 0 to Opaque, yet whenever I run this script, the texture is changed instantly without any interpolation in between. What am I doing wrong?
#pragma strict
var BookIcon : GUITexture;
function Start ()
{
BookIcon.color.a = Mathf.Lerp(0,8,5 * Time.deltaTime);
}
Answer by tanoshimi · Nov 14, 2013 at 10:56 PM
"What am I doing wrong?" Several things...
You've placed your code in Start(), so you're only setting color.a once. If you want it to fade, you're gonna have to update its value several times, right? Like in an Update() function?
Lerp(a,b,c) returns a value somewhere between a and b, depending on how close c is from 0 to 1. Your value of c is 5 Time.deltaTime*. Deltatime is the amount of time passed since the last frame. So it might be 0.03 one frame, then 0.025 the next, then 0.031 etc. You're then multiplying by 5, which means you're setting BookIcon.color.a to a fairly arbitrary fraction of the way from 0 to 8.
The alpha component of a colour is a floating point value from 0 to 1. Even if you got your lerp to work, you'd be smoothly transitioning from 0 to 8...
And finally, you didn't search before you posted, which meant you didn't find the previous times this question has been answered. Like here: http://answers.unity3d.com/questions/406507/making-guitexture-start-transparent-then-fade-in.html (but put it in Update(), not OnGUI())
Thank you, that cleared up everything, and I appreciate the detailed answer.