Is this the easiest way to change the transparency of a Slider?

I’ve pieced together some code intending to change the transparency of a slider component, so that rather than just making it vanish I can tween its transparency for fading out. I’m having two issues though. One, I think is a bug and the other is with how verbose my code is. First, the bug. The below code works fine for changing the opacity of my slider to 0 (when toOpacity is 0). But when “toOpacity” is 150 the opacity on the image component isn’t changed to 150. It’s changed to 255. Is this a bug? Or am I doing something wrong.

Image manaImage = Mana.GetComponent<Image>();
Image manaFillImage = Mana.transform.FindChild("Fill Area/Fill").GetComponent<Image>();

manaFillImage.color = new Color(manaFillImage.color.r, manaFillImage.color.g, manaFillImage.color.b, toOpacity);
manaImage.color = new Color(manaImage.color.r, manaImage.color.g, manaImage.color.b, toOpacity);

And finally, is there a simpler way to fade my slider to invisible and back to visibility without having to fade two different objects at the same time? Something like an Opacity property on the ICanvasElement interface that would affect the opacity on all child elements would be awesome but I’m afraid that’s asking too much.

Anyone?

You’re using the Color struct. Have you read the docs for the Color struct?
http://docs.unity3d.com/460/Documentation/ScriptReference/Color.html

Yes, you can use a CanvasGroup component.

Crap you got me this time. But in my defense, the inspector uses a value between 0 and 255. I therefore assumed I would use the same values programatically

Perfect! Thanks

Yep, it’s a fair assumption to make. :slight_smile:

Personally, when something doesn’t work the way I expect, I strive to start out by referring to available material (like documentation) to make sure my assumptions are correct before beginning to report issues online. But it probably slips once in a while for me as well…