I’ve been at this for an hour now and I’m starting to lose my mind.
trying to make a simple blood overlay that is set active by an outside object, that reduces in alpha until its at 0 alpha, at which point it disables itself. Instead, it seems to only do two states for the alpha: fully on, or fully off. It disables and enables like it should, and even seems to detect when the Alpha should be 0 to turn itself off, but it doesn’t do a proper fade-out.
Here’s my code
public float alphaReduce=1f;
public Image myImage;
void OnEnable()
{
Color c = myImage.color;
c.a = 255;
myImage.color = c;
}
void Update () {
Color c = myImage.color;
c.a =c.a - alphaReduce;
myImage.color = c;
if (myImage.color.a <= 0) gameObject.SetActive(false);
}
Remember that Unity’s build in Color takes r,g,b,a 0 to 1. I wrote sth like this:
public class Fade : MonoBehaviour {
public Image myImage;
public Color fadeColor; //you could arrange fade color from inspector too i assigned it manually here
void Start () {
//this line here is unnecessary if you assign fadeColor from inspector
fadeColor = new Color(myImage.color.r, myImage.color.g, myImage.color.b, 0f);
}
void Update () {
myImage.color = Color.Lerp(myImage.color, fadeColor, Time.deltaTime);
if (myImage.color.a < 0.02) { //having a threshold is a better option
gameObject.SetActive(false);
}
}
}