Search Unity

changing alpha of a text

Discussion in 'Getting Started' started by witcher101, Sep 23, 2016.

  1. witcher101

    witcher101

    Joined:
    Sep 9, 2015
    Posts:
    516
    How do i change alpha of text. I am using this but doesnt seem to work
    Code (CSharp):
    1. public Text healthtext;
    2.  
    3. healthtext.text = healthpack.ToString();
    4.         Color zm = healthtext.color;
    5.         zm.a = 0.0f;
     
    oba_odelana likes this.
  2. pele90

    pele90

    Joined:
    Feb 25, 2015
    Posts:
    2
    Well I guess that is not all the code you do to change alpha of text?!
    Do you set the color of healthtext to the color you instantiated?
    Meaning:
    Code (CSharp):
    1. healthtext.color = zm;
     
    cristobalriaga and JoeStrout like this.
  3. Deleted User

    Deleted User

    Guest

    I got one, woot! Good post, good question. Good OP.

    So you have the alpha of the material, but you also have the alpha of the Canvas Renderer, which we can think of like a Mesh Renderer but it renders any component on the Canvas it's attached to. Look at a Text object in your hierarchy and you should find one.

    Text.CrossFadeAlpha lets you change the alpha of an entire Text object really easily. Try that instead.

    Also aside, let me further elaborate:

    Code (CSharp):
    1. public Text healthtext;
    2. healthtext.text = healthpack.ToString();
    3.         Color zm = healthtext.color;  //  makes a new color zm
    4.         zm.a = 0.0f; // makes the color zm transparent
    You'll need to also set the color of the Text's material to zm!

    Don't feel bad, I still haven't stopped using the equals sign (or "assignment operator") incorrectly sometimes!
     
    Mike2699 and cristobalriaga like this.
  4. Vitassa

    Vitassa

    Joined:
    Mar 21, 2020
    Posts:
    1
    Congratulations you saved a life going to the halo!!!