Search Unity

  1. Engage, network and learn at Unite Austin 2017, Oct 3 - 5. Get your ticket today!
    Dismiss Notice
  2. Introducing the Unity Essentials Packs! Find out more.
    Dismiss Notice
  3. Check out all the fixes for 5.6 on the patch releases page.
    Dismiss Notice
  4. Unity 2017.1 is now released.
    Dismiss Notice
  5. Unity 2017.2 beta is now available for download.
    Dismiss Notice

Help with fading in text

Discussion in 'Getting Started' started by Iqew, Sep 12, 2017.

  1. Iqew

    Iqew

    Joined:
    Nov 26, 2016
    Posts:
    32
    So i'm trying to make text fade in once the player collides with the object. This is my code but for some reason, my text isn't appearing even though it says in the debug log that the alpha is getting increased.
    Please tell me if there is anymore information that I need to provide. I would gladly do anything to get a good answer.

    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4.  
    5. public class PersonaScript : MonoBehaviour {
    6.  
    7.     public string messageText;
    8.     public float Delay;
    9.     public Text Ptext;
    10.     public GameObject Message;
    11.     private Color MatColor;
    12.  
    13.  
    14.  
    15.     void Start () {
    16.         Ptext.text = messageText;
    17.         MatColor = Ptext.color;
    18.         MatColor.a = 0;
    19.        
    20.     }
    21.    
    22.     void OnTriggerEnter (Collider other) {
    23.         if (other.CompareTag ("Player")){
    24.             StartCoroutine("FadeIn");
    25.         }  
    26.     }
    27.  
    28.     IEnumerator FadeIn(){
    29.         while(MatColor.a <= 1){
    30.             Debug.Log(MatColor.a);
    31.             MatColor.a += .3f;
    32.             yield return new WaitForSeconds(.2f);
    33.         }
    34.         yield return new WaitForSeconds(Delay);
    35.     }
    36.  
    37. }
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    2,881
    You must assign the colour back to the text's colour.
    Color is a struct (therefore, copied on assignment - not referenced). You are updating 'MatColor' 's alpha property, but the text color doesn't know that you've done that.. until you assign it back :)
     
  3. Iqew

    Iqew

    Joined:
    Nov 26, 2016
    Posts:
    32
    Thank you very much. Now i've got the text fade fully working :)
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    2,881
    Cool :) Glad ya got it..
     
  5. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    5,616
    Glad you got it working. FYI, though, you really don't need to work this hard to fade in text. You can just call CrossFadeAlpha on it, and magic happens. :)
     
  6. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    2,881
    Thanks for reminding me about that ;)
     
  7. Iqew

    Iqew

    Joined:
    Nov 26, 2016
    Posts:
    32
    Thanks for the advice but may I ask how I should use it? I read the documentation and tried changing my script. It doesn't work. I'd really like to simplify my code.
    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4.  
    5. public class PersonaScript : MonoBehaviour {
    6.  
    7.     public string messageText;
    8.     public float Delay;
    9.     public Text Ptext;
    10.     public GameObject Message;
    11.     private Color MatColor;
    12.  
    13.  
    14.  
    15.     void Awake () {
    16.         Ptext.text = messageText;
    17.         MatColor = Ptext.color;
    18.         MatColor.a = 0;
    19.         Ptext.color = MatColor;
    20.     }
    21.    
    22.     void Update(){
    23.        
    24.     }
    25.    
    26.     void OnTriggerEnter (Collider other) {
    27.         if (other.CompareTag ("Player")){
    28.             Ptext.CrossFadeAlpha(1, 1.5f, false);
    29.            // StartCoroutine("FadeIn");
    30.         }  
    31.     }
    32.  
    33.     IEnumerator FadeIn(){
    34.         while(MatColor.a <= 1){
    35.             Debug.Log(MatColor.a);
    36.             MatColor.a += .2f;
    37.             Ptext.color = MatColor;
    38.             yield return new WaitForSeconds(.5f);
    39.         }
    40.         yield return new WaitForSeconds(Delay);
    41.     }
    42.  
    43. }
     
  8. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    5,616
    This is a common point of confusion — Graphic.CrossFadeAlpha does not operate on the color of the Graphic (the Image component in this case), like you might expect. Instead it operates on the alpha of the CanvasRenderer component, which is easy to overlook since it's not a very accessible property. (It's exposed only via the GetAlpha and SetAlpha methods, and not visible in the Inspector.)

    But anyway, here's how to fix your script:
    1. Change the color of your Text object back to a nice solid (opaque) color.
    2. In the Start method of your script, add code like this:
    Code (CSharp):
    1. // make Ptext initially invisible
    2. Ptext.GetComponent<CanvasRenderer>().SetAlpha(0);
    That's it. Then of course you can delete most of the rest of your script — all you need is the CrossFadeAlpha call to make it fade back in.