I may think is probably because the function Mathf.Lerp(minimum, maximum, Time.time); that should go from a min value to a max. In your case when you perform Fade out you are going from a min value of 5 to 0. This is my guess
EDIT: Ok sorry what I said in this post is not true
So im having a problem with my fade in function, I came here and this page was very helpful but i cant get mine to work. Its a javascript attached to a GUITexture. I just need the fade in ability I’m not using the fade out here is the code im using:
function Start () {
yield Fade(0, .5, 1); // Start, end, length in seconds
}
function Fade (start : float, end : float, length : float) {
for (i = 0.0; i < 1.0; i += Time.deltaTime*(1/length)) {
guiTexture.color.a = Mathf.Lerp(start, end, i);
yield;
}
}
as you can see the above was very inspiring but when i start the game my image dosent fade. Any help here would be greatly appreciated.
Well I have used this one several times both for iPhone and PC/Mac use.
var guiObject : GUITexture;
var fadeTime = 1.0;
enum Fade {In, Out}
// Fade in the GUITexture, wait a couple of seconds, then fade it out
function Start () {
yield FadeGUITexture(guiObject, fadeTime, Fade.In);
yield WaitForSeconds(2.0);
yield FadeGUITexture(guiObject, fadeTime, Fade.Out);
Application.LoadLevel (Application.loadedLevel+1);
}
function FadeGUITexture (guiObject : GUITexture, timer : float, fadeType : Fade) {
var start = fadeType == Fade.In? 0.0 : 1.0;
var end = fadeType == Fade.In? 1.0 : 0.0;
var i = 0.0;
var step = 1.0/timer;
while (i < 1.0) {
i += step * Time.deltaTime;
guiObject.color.a = Mathf.Lerp(start, end, i)*.5;
yield;
}
}
Thanx andy_h for the quick response and help but unfortunately it never worked…maybe i did something wrong with the code. I put in your code almost exactly, I did remove these 2 lines:
only because Im not useing a fade out and im not loading another level but other then that i kept it the same. I never got any errors or any wierd glitches it just never faded. Any more help would be greatly appreciated. Also im kinda a noob so please bare with me, thanks
Thanx again but still no change maybe it would help if I posted my all my code:
var MaskTexture : Texture2D;
static var depth : int;
var guiObject : GUITexture;
var fadeTime = 5.0;
enum Fade {In, Out}
function Update () {
}
// Fade in the GUITexture, wait a couple of seconds, then fade it out
function Start () {
yield FadeGUITexture(guiTexture, fadeTime, Fade.In);
yield WaitForSeconds(2.0);
//yield FadeGUITexture(guiObject, fadeTime, Fade.Out);
// Application.LoadLevel (Application.loadedLevel+1);
}
function FadeGUITexture (guiObject : GUITexture, timer : float, fadeType : Fade) {
var start = fadeType == Fade.In? 0.0 : 1.0;
var end = fadeType == Fade.In? 1.0 : 0.0;
var i = 0.0;
var step = 1.0/timer;
while (i < 1.0) {
i += step * Time.deltaTime;
guiObject.color.a = Mathf.Lerp(start, end, i)*.5;
yield;
}
}
The image is an element for a HUD. I also moved the onGUI function below the Update function but above the start function to see if it was because of what you suggested it being below the fade function but again no change, the image still stays regular with no fade.
sure thanx again for the help also not sure if this will help but before i used the onGUI to position the image I placed it on screen and positioned it useing the transform panel then used the fade in code I put up on my first comment and it worked no problem, but as soon as I put in the onGUI function for positioning and scaleing the fade stopped working…not sure if this helps but thought Id let you know just in case thanx again
// Fade in the GUITexture, wait a couple of seconds, then fade it out
function Start () {
yield FadeGUITexture(guiTexture, fadeTime, Fade.In);
yield WaitForSeconds(2.0);
//yield FadeGUITexture(guiObject, fadeTime, Fade.Out);
// Application.LoadLevel (Application.loadedLevel+1);
}
function FadeGUITexture (guiObject : GUITexture, timer : float, fadeType : Fade) {
var start = fadeType == Fade.In? 0.0 : 1.0;
var end = fadeType == Fade.In? 1.0 : 0.0;
var i = 0.0;
var step = 1.0/timer;
while (i < 1.0) {
i += step * Time.deltaTime;
guiObject.color.a = Mathf.Lerp(start, end, i)*.5;
yield;
}
}