- Home /
 
How to make my label fade after 2 sec?
i want to create a label that will show on a certain condition and will fade after 2 sec just like for example on the online game the damage that comes out then fades out .. how can i do that by the way im using c# please help me thank you
               Comment
              
 
               
               
               Best Answer 
              
 
              Answer by CodeElemental · Mar 03, 2014 at 01:19 PM
 public class LabelDisplay : MonoBehaviour
 {
     public float DisplayTime = 2; // Will be displayed for 2 seconds.
     public float FadeoutTime = 1; // Will fade out afterwards for 1 second.
 
     private bool displayTimePassed = false;
     private bool fadeOutTimePassed = false;
     private bool isShown = false;
     float elapsedTime = 0f;
     float labelAlpha = 1f;
     
     private string TextToShow = "Demo";
     void Update() 
     {
        if (isShown)
        {
             elapsedTime += Time.deltaTime;
             if (!displayTimePassed) // We still haven't finished displaying the "full opacity version"
             {
                 if (elapsedTime > DisplayTime)
                 {
                    elapsedTime = 0;
                    displayTimePassed = true;
                 }
             } else if (!fadeOutTimePassed) // We still haven't finished displaying the fade-out.
             {
                 labelAlpha = (float) ((FadeoutTime - elapsedTime) / FadeoutTime);
                 if (elapsedTime > FadeoutTime)
                 {
                    elapsedTime = 0;
                    fadeOutTimePassed = true;
                 }
             } else // Display and fadeout have passed.
             {
                 isShown = false;
                 // Possibly destroy object when done?
                 // Destroy(gameObject); 
                 // NOTE : This must be a clone object (created via Instantiate call) so that you don't destroy the main Label prefab.
                 // If you destroy the "original" prefab you cannot instantiate that kind of prefabs anymore.
             }
         }
     }
 
     public void Show(string text)
     {
         TextToShow = text;
         isShown = true;
         // Possibly refresh intervals?
         // displayTimePassed = false;
         // fadeOutTimePassed = false;
         // elapsedTime = 0f;
     }
     // Possible overrides Show(string text, displayTime , fadeouttime)
 
     void OnGUI()
     {
         if (isShown)
         {
             if (Mathf.Approximately(labelAlpha, 1f))
             {
                 GUI.Label (Rect(0,0,<your width>, <your height>), TextToShow) ;
             } else 
             {
                 // Set alpha before label draw (for fade)
                 GUI.color.a = labelAlpha;
                 GUI.Label (Rect(0,0,<your width>, <your height>), TextToShow) ;
                 GUI.color.a = 1f;
             }
         }
     }
 }
 
               However for a more future-proof solution ( GUI framework ) i recommend using NGUI, or if you already use it , you should take a look at HudText
Your answer