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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

CrossFadeAlpha Issues

Discussion in 'Scripting' started by HarryMcCurly, Feb 12, 2018.

  1. HarryMcCurly

    HarryMcCurly

    Joined:
    Jul 18, 2017
    Posts:
    6
    Hi,
    I'm trying to use CrossFadeAlpha to fade in UI text in the beginning and fade out after some seconds. Fade in works just fine but fading out again doesn't do what it should be.

    If duration set to 1f it fades maybe 10%. If duration set to 0f it fades 100% - of course immediately. The duration actually fades the object, not alpha. When alpha is set to 1f obviously nothing happens because it fades from 1f to 1f.

    What is it that CrossFadeAlpha actually controls? Because when I print out the alpha value in the Debug.Log it continuously says 1f, even though fade in at the beginning is working.

    Code (CSharp):
    1.  
    2.     public float counter = 6f;
    3.     public Text Txt1;
    4.  
    5.     void Start()
    6.     {
    7.         Txt1.CrossFadeAlpha (0f, 0f, false);
    8.     }
    9.  
    10.     void Update ()
    11.     {
    12.         counter -= Time.deltaTime;
    13.  
    14.         if (counter < 4)
    15.         {
    16.             Txt1.CrossFadeAlpha (1f, 1f, false);
    17.         }
    18.  
    19.         if (counter <= 0)
    20.         {
    21.             Txt1.CrossFadeAlpha (0f, 1f, false);
    22.         }
    23.     }
    Could there be another way?
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    It fades that CanvasRenderer alpha.
    I would suggest that you update your logic so it's more like this:
    Code (csharp):
    1. if (counter < 4 && counter > 0) // for the first one
    See if that helps.
    Currently your code has 2 statements that evaluate to true.
     
  3. HarryMcCurly

    HarryMcCurly

    Joined:
    Jul 18, 2017
    Posts:
    6
    Thank you. That's a solution. It still behaves different from the first one. I wonder if my code is inefficient. At least it works. That's all I need for now.
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    You're welcome. You can try this:
    Code (csharp):
    1. [SerializeField]
    2. Text someText;
    3.  
    4. void Start () {
    5.    StartCoroutine(FadeInOut(2,4));
    6. }
    7.    
    8. IEnumerator FadeInOut(float wait1, float wait2)
    9. {
    10.    someText.CrossFadeAlpha(0, 0, false);
    11.    yield return new WaitForSeconds(wait1);
    12.    someText.CrossFadeAlpha(1, 1, false);
    13.    yield return new WaitForSeconds(wait2);
    14.    someText.CrossFadeAlpha(0, 1, false);
    15. }
     
  5. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
    The problem with your original code was likely that you start the fade over again every frame causing this undesired behaviour, what you need is to only call it once. @methos5k gives an example like that using a coroutine, be careful not to start too many of those aswell.
     
  6. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Ya, I think the coroutine is much better. When I first responded, I didn't fully think it through (restarting it each frame). :)