- Home /
This post has been wikified, any user with enough reputation can edit it.
Question by
lukacat10 · Nov 25, 2014 at 06:39 AM ·
scripting problem
UI Image component Alpha Lerp doesnt work well
Ok, so I wrote a code to fade a UI when a variable is changed by a 3rd side script. I got to a clean Console with no errors. BUT, it still does'nt work. Instead of fading it actually just moves from a 0 alpha to 1 alpha without the lerp that I actually initialized. I tried to use Mathf.Lerp, but didnt work either. Here is the code:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class setActive : MonoBehaviour {
public bool KeepRunning = false;
//bool nowMax = false;
public GameObject restartButton;
public float Speed;
// Use this for initialization
Color colorMinA = new Color(1,1,1,0);
void Start () {
restartButton.GetComponent<Image>().color = colorMinA;
}
// Update is called once per frame
void Update () {
if (KeepRunning == true/* && nowMax == false*/) {
restartButton.GetComponent<Image>().color = Color.Lerp(restartButton.GetComponent<Image>().color, new Color(1,1,1, 1), Time.deltaTime * Speed);
//nowMax = true;
} else if (KeepRunning == false/* && nowMax == true*/) {
restartButton.GetComponent<Image>().color = Color.Lerp(restartButton.GetComponent<Image>().color, new Color(1,1,1, 0), Time.deltaTime * Speed);
//nowMax = false;
}
}
}
Ignore the "nowMax" The keeprunning variable controls if the alpha of the ui image component fades out or in. I looked at the code for a long time and didnt find the problem.
Comment
Perhaps this thread could help you out? http://answers.unity3d.com/questions/328891/controlling-duration-of-colorlerp-in-seconds.html