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.

Bug with alpha?

Discussion in 'UGUI & TextMesh Pro' started by Deleted User, Aug 21, 2014.

  1. Deleted User

    Deleted User

    Guest

    Hi guys.
    I need to make a fade-out effect for my label.
    Here is my code:

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4.  
    5. public class TweenAlpha : MonoBehaviour
    6. {
    7.     public float Speed;
    8.     private Text Label;
    9.     private float alpha;
    10.     private Color color;
    11.  
    12.     /* -------- */
    13.  
    14.     void Start()
    15.     {
    16.         Label = GetComponent<Text>();
    17.         if(Label == null)
    18.             this.enabled = false;
    19.         color = Label.color;
    20.     }
    21.  
    22.     /* -------- */
    23.  
    24.     void Update()
    25.     {
    26.         alpha += Time.deltaTime / Speed;
    27.         color.a = alpha;
    28.  
    29.         Label.color = color;
    30.     }
    31.  
    32.     /* -------- */
    33. }
    34.  
    For example:
    Speed = 3 (Fade-in effect)
    Speed = -3 (Fade-out effect).

    As i said i need to make a fade-out effect,but here is my problem : the alpha value of my label becomes immediately...let's say 'zero'.
    However, fade-in effect works fine with this script.
     
  2. pixelballoon

    pixelballoon

    Joined:
    Mar 5, 2013
    Posts:
    11
    That's because you haven't assigned alpha a value so it will always be initialised to zero.

    Rather than using a local variable you could just do:

    Code (CSharp):
    1. void Update()
    2. {
    3.     color.a = color.a + Time.deltaTime / Speed;
    4.     Label.color = color;
    5. }
    6.  
    There's potentially a few other issues in that code though, like there's no end to the fade etc...
     
    Tim-C likes this.
  3. Deleted User

    Deleted User

    Guest

    Thanks! It works now.
    Well,this is just a test ;)