Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

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

Discussion in 'UGUI & TextMesh Pro' started by omatase, Nov 8, 2014.

  1. omatase

    omatase

    Joined:
    Jul 31, 2014
    Posts:
    159
    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.

    Code (CSharp):
    1. Image manaImage = Mana.GetComponent<Image>();
    2. Image manaFillImage = Mana.transform.FindChild("Fill Area/Fill").GetComponent<Image>();
    3.  
    4. manaFillImage.color = new Color(manaFillImage.color.r, manaFillImage.color.g, manaFillImage.color.b, toOpacity);
    5. 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.
     
  2. omatase

    omatase

    Joined:
    Jul 31, 2014
    Posts:
    159
    Anyone?
     
  3. runevision

    runevision

    Joined:
    Nov 28, 2007
    Posts:
    1,870
    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.
     
  4. omatase

    omatase

    Joined:
    Jul 31, 2014
    Posts:
    159


    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
     
  5. runevision

    runevision

    Joined:
    Nov 28, 2007
    Posts:
    1,870
    Yep, it's a fair assumption to make. :)

    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...