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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

[SOLVED] Fading UI Text?

Discussion in 'Scripting' started by RPP, Dec 1, 2015.

  1. RPP

    RPP

    Joined:
    Nov 11, 2015
    Posts:
    28
    Hello, I've been doing research on how to write a script to fade my UI Text i have in a Unity Scene. However, everything i find is related to the old Unity <GUIText>. If someone could help me figure out how to write a new script for UI or point me in a direction that would be great! Here is an old GUI text fade script i found while following tutorials, its irrelevant now i assume with unity 5.x

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class UIfadeOutText : MonoBehaviour
    5. {
    6.  
    7. const float period = 4.0f;
    8.  
    9. void Update ()
    10.     {
    11.         if(Time.time > period)
    12.         {
    13.             Destroy(gameObject);
    14.         }
    15.            
    16.             Color colorOfObject = GetComponent<GUIText>().material.color;
    17.  
    18.             float prop = (Time.time / period);
    19.  
    20.             colorOfObject.a = Mathf.Lerp(1, 0, prop);
    21.  
    22.             GetComponent<GUIText>().material.color = colorOfObject;
    23.     }
    24. }
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    add a "canvas group" component (from "layout" menu group), change it's alpha value.
     
  3. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,256
    Not too much to change.

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;//Added this
    3. using System.Collections;
    4. public class UIfadeOutText : MonoBehaviour
    5. {
    6. const float period = 4.0f;
    7. void Update ()
    8.     {
    9.         if(Time.time > period)
    10.         {
    11.             Destroy(gameObject);
    12.         }
    13.          
    14.             Color colorOfObject = GetComponent<Text>().color;//Changed this
    15.             float prop = (Time.time / period);
    16.             colorOfObject.a = Mathf.Lerp(1, 0, prop);
    17.             GetComponent<Text>().color = colorOfObject;//Changed this
    18.     }
    19. }
     
  4. RPP

    RPP

    Joined:
    Nov 11, 2015
    Posts:
    28
    ahhh, i see the whole reason i couldn't figure out how to change the UI text was because i wasn't including the unity UI engine library... I literally wrote 4 source codes wondering WTH am i doing wrong. Thanks so much for responding Chris!.