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

Changing a (new) UI Image's alpha value

Discussion in '2D' started by LeeDenbigh, Jan 8, 2015.

  1. LeeDenbigh

    LeeDenbigh

    Joined:
    Jul 7, 2014
    Posts:
    48
    Hi, I am new to Unity.

    I have 3 images on my UI that are set at 0.5 alpha. When the player collects a collectable in the game, I would like one of the images to turn to full alpha, then the same for collectable 2 and so on...

    I'm sure it's a easy process, but I just can't seem to work it out.

    Help would be very much appreciated.

    Thanks.
     
    jplr98 likes this.
  2. BenZed

    BenZed

    Joined:
    May 29, 2014
    Posts:
    524
    Be sure to check the documentation before asking here! You'll get your answers much quicker :)

    That being said:


    Code (CSharp):
    1. using UnityEngine.UI;
    2.  
    3. public class Example : MonoBehaviour {
    4.     Image image;
    5.  
    6.     void Start() {
    7.         image = GetComponent<Image>();
    8.         image.color = Color.white; //or whatever color
    9.     }
    10.  
    11. }
     
    CaelumCB and LeeDenbigh like this.
  3. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    897
    Wouldn't it be better to actually set the alpha, instead of changing color?

    Code (CSharp):
    1. using UnityEngine.UI;
    2. public class Example : MonoBehaviour {
    3.     Image image;
    4.     void Start() {
    5.         image = GetComponent<Image>();
    6.  
    7.         Color c = image.color;
    8.         c.a = 0;
    9.         image.color = c;
    10.     }
    11. }
     
  4. LeeDenbigh

    LeeDenbigh

    Joined:
    Jul 7, 2014
    Posts:
    48
    Thanks for the replies. PGJ, yours was exactly what I wanted, cheers!
     
  5. EDevJogos

    EDevJogos

    Joined:
    Jul 24, 2014
    Posts:
    73
    You can create an extension, so that you can use anywhere on your code:

    Code (CSharp):
    1. public static class Extensions
    2. {
    3.     public static void SetTransparency(this UnityEngine.UI.Image p_image, float p_transparency)
    4.     {
    5.         if (p_image != null)
    6.         {
    7.             UnityEngine.Color __alpha = p_image.color;
    8.             __alpha.a = p_transparency;
    9.             p_image.color = __alpha;
    10.         }
    11.     }
    12. }
    Then you can just call this from anywhere:

    Code (CSharp):
    1. button.GetComponent<Image>().SetTransparency(float value);
     
  6. SuperBob132

    SuperBob132

    Joined:
    May 24, 2017
    Posts:
    1
    What version of unity is this?
     
    Khyrid likes this.
  7. CrimOudin

    CrimOudin

    Joined:
    Apr 13, 2016
    Posts:
    3
    Why doesn't this work for setting the alpha? My health bars are constantly 255 alpha. They do change to the correct colors though.

    Code (CSharp):
    1.   private void UpdateHpImage()
    2.   {
    3.     var percentHP = (float)Data.CurrentHP / Data.MaxHP;
    4.     hpImage.fillAmount = percentHP;
    5.     if(percentHP == 1)
    6.     {
    7.       hpImage.gameObject.SetActive(false);
    8.     }
    9.     else
    10.     {
    11.       hpImage.gameObject.SetActive(true);
    12.     }
    13.     if(percentHP > .5f)
    14.     {
    15.       hpImage.color = new Color(0, 255, 0, 45);
    16.     }
    17.     else if(percentHP <= .5f && percentHP > .25f)
    18.     {
    19.       hpImage.color = new Color(255, 255, 0, 45);
    20.     }
    21.     else if(percentHP <= .25f)
    22.     {
    23.       hpImage.color = new Color(255, 0, 0, 45);
    24.     }
    25.   }
     
  8. CrimOudin

    CrimOudin

    Joined:
    Apr 13, 2016
    Posts:
    3
    I guess The a in color for some reason is a float between 0 and 1 for transparency.
     
  9. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637