- Home /
Text.CrossFadeAlpha not working?
I'm trying to fade in a UI component by using the following code:
public float fadeTime = 1.0f;
public Text textItemGUI; // Set through inspector.
void Start()
{
textItemGUI.CrossFadeAlpha(1.0f, fadeTime, true);
}
Nothing happens! I'm logging the alpha in the update function and there I can see that the alpha is just kept 0.0 forever. What is wrong?
Answer by ben-rasooli · May 01, 2017 at 01:37 AM
Graphic.CrossFadeAlpha() changes the alpha of the CanvasRenderer attached to the same gameObject, not the alpha of the Image.color. If you wanna start your image completely transparent and then fade it in, you need to set the alpha of its CanvasRenderer to zero like this imageComponent.canvasRenderer.SetAlpha(0);
.Don't change the alpha of the Image.color.
This should be the accepted answer. It is correct both in what you should do, and in the explanation as to why.
This isn't working for a Text$$anonymous$$eshPro.CrossFadeAlpha(1f, 2f, false);
It just never fades back in after setting the canvasRenderer's alpha to 0. Any ideas?
Answer by Jeff_B · Jul 15, 2015 at 07:28 PM
Hellium fixed it for me. I added one line and now the following "fade in" line works! :
(...)
faderGraphic.canvasRenderer.SetAlpha(0.01);
faderGraphic.CrossFadeAlpha(1.0, fadeSpeed, false);
(...)
This is great! Thanks to whomever posted that fix. I hardly use the canvasRenderer class for anything. For those learning to use this in C#, just be sure to add a lowercase "f" (SetAlpha(0.01f)).
I hope the docs will soon reflect that this value can not be zero to begin with - or else it just won't work.
I also hope that the class itself will eventually be updated to check if the alpha == 0, ins$$anonymous$$d of just not fading if so, with no errors.
If anyone reading wonders why this solution with CrossFadeAlpha() works the way it does:
It is because when scripting in some cases, unity does not work with the default RGB values of 0 - 255. But rather a 0 - 1 float number representing the range of the value. (1f = 255).
You don't need to set the alpha in Awake(), but you do have to set the alpha as a starting point before using CrossFadeAlpha inside your function.
This is a correct answer, but the other comments about it are incorrect. It works fine with an alpha of 0. And it has nothing to do with floats vs. integers. The confusion is that CrossFadeAlpha operates on the CanvasRenderer component's alpha value, not on the color of the Graphic you call it on.
Thank you JoeStrout. Yes, that explains everything. The fix in my case was not changing the Image.color, but rather the Image.canvasRenderer.color when I initialize. Then CrosFadeAlpha works perfectly!
Your answer
Follow this Question
Related Questions
crossfade between skyboxes 1 Answer
Animation.isPlaying, but animation isn't playing 0 Answers
Unity 3.5 Animation : stop one layer 1 Answer
How to make CrossFade between 2 camera? 0 Answers
CrossFade with script? 1 Answer