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

Trouble with a script made to fade in and out text

Discussion in 'Scripting' started by agentc0re, Jun 23, 2015.

  1. agentc0re

    agentc0re

    Joined:
    Feb 28, 2014
    Posts:
    77
    Hello, my goal is to achieve a constant fading in and display of text. Not quite blinking, just enough to get the attention of the user.

    My setup:
    I currently have a script which when the user dies, a UI interface pops up. In that UI i have text in which i want to fade in and out continuously until the user presses a key to move on and clicks the retry button.

    Here is the script i came up with to try and accomplish this.
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5. public class FadeInFadeOut : MonoBehaviour {
    6.  
    7.  
    8.     // Use this for initialization
    9.     void Start ()
    10.     {
    11.      
    12.     }
    13.  
    14.     // Update is called once per frame
    15.     void Update ()
    16.     {
    17.  
    18.     }
    19.     void FadeIn()
    20.     {
    21.         while (gameObject.GetComponent<Text>().color.a < 0.99f)
    22.         {
    23.             gameObject.GetComponent<Text>().color = new Color(gameObject.GetComponent<Text>().color.r, gameObject.GetComponent<Text>().color.g, gameObject.GetComponent<Text>().color.b, Mathf.Lerp(gameObject.GetComponent<Text>().color.a, 1.0f, Time.deltaTime * 2.5f));
    24.             Debug.Log("incrementing alpha");
    25.         }
    26.         gameObject.GetComponent<Text>().color = new Color(gameObject.GetComponent<Text>().color.r, gameObject.GetComponent<Text>().color.g, gameObject.GetComponent<Text>().color.b, 1.0f);
    27.         Debug.Log("alpha = 1");
    28.     }
    29.     void FadeOut()
    30.     {
    31.         while (gameObject.GetComponent<Text>().color.a > 0.01f)
    32.         {
    33.             gameObject.GetComponent<Text>().color = new Color(gameObject.GetComponent<Text>().color.r, gameObject.GetComponent<Text>().color.g, gameObject.GetComponent<Text>().color.b, Mathf.Lerp(gameObject.GetComponent<Text>().color.a, 0.0f, Time.deltaTime * 2.5f));
    34.             Debug.Log("decrementing alpha");
    35.         }
    36.         gameObject.GetComponent<Text>().color = new Color(gameObject.GetComponent<Text>().color.r, gameObject.GetComponent<Text>().color.g, gameObject.GetComponent<Text>().color.b, 0.0f);
    37.         Debug.Log("alpha = 0");
    38.     }
    39. }
    ----------------------
    This script is placed on the UI Text component that i want to fade in and out.
    To test this out i first put the FadeIn() and FadeOut() functions in the Start() function. When i do this, it prevents my UI from popping up until the fading in and out is done. I know this because if i call FadeOut() last, the text in question does not appear. When I call FadeIn() the text appears once the UI loads.

    Is there an easier way to try and go about doing this? Why is my code preventing the UI from appearing until it finishes fading in and out in the background?

    Thank you for any help/guidance you can provide!
    -Jon
     
  2. sluice

    sluice

    Joined:
    Jan 31, 2014
    Posts:
    416
  3. startas

    startas

    Joined:
    Nov 14, 2014
    Posts:
    102
    You need to use some tweening assets, like DOTween, very good asset, has free version, or you need to understand the very basics of programming - now you are fading your text in one function call, also, that function is not update, so there is no new frames between it, also, you should forget loops like "while" in unity - it just makes troubles for new programmers and they get stuck.
     
  4. WheresMommy

    WheresMommy

    Joined:
    Oct 4, 2012
    Posts:
    890
    Why don't you just make an animation for that textfield and set it to loop? Seems the easiest way to achieve what you want without fiddeling with code you do not really need I guess?
     
  5. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    those two functions will complete in a single frame, there is no yield instructions. I think you are trying to make a coroutine, but you're missing all the syntax that make them work :confused:

    https://unity3d.com/learn/tutorials/modules/intermediate/scripting/coroutines


    also, don't repeatedly call GetComponent<>() to get the same thing over and over, store it in a variable and then use the variable.
     
  6. agentc0re

    agentc0re

    Joined:
    Feb 28, 2014
    Posts:
    77
    @sluice Thanks man, that's a great suggestion. Gave it a look over, and tried it out. I am able to easily fade out no problem, but right now i'm struggling on trying to figure out how to repeat the process by fading it back in and then back out... etc. etc.

    @asd234w4r5 I don't know how to do that, but i'll look into it and see what i can figure out.

    @LeftyRighty You're absolutely right. I needed to make them coroutines, which I did and it well... sorta worked.

    My problem now is that if i use the DOTween or coroutine method, the audio that plays when that UI pops up becomes distorted. Since i wanted to "freeze" time when you die so nothing else can happen, it prohibits the fading to occur.

    This is the code i call to pop up the GUI for when the player loses. I've had to comment out the audio parts because of the issue when fading it causes the audio to sound robotic IE. EEERRRRRRRRRRRR. Continuously.
    Code (CSharp):
    1.  IEnumerator LoseGUI()
    2.     {
    3.         yield return new WaitForSeconds(2.3f);
    4.         //Debug.Log("Entered LoseGUI and attempting to play sounds");
    5.         //GetComponent<AudioSource>().clip = loseClip;
    6.         //GetComponent<AudioSource>().Play();
    7.         //Time.timeScale = 0.0f;
    8.         //GUImode = "gaming";
    9.         UIPanelLose.SetActive(true);      
    10.     }
    My questions:
    Is there another way to stop time like i am without it preventing text fading in and out to work? Why is my audio sounding awful when the fading of text happens?

    Thank you!
    -Jon

    Let me know if you need me to paste any more code.
     
  7. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
  8. sluice

    sluice

    Joined:
    Jan 31, 2014
    Posts:
    416
    @agentc0re This should do the trick with DOTween:
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4. using DG.Tweening;
    5.  
    6. public class BlinkingText : MonoBehaviour
    7. {
    8.     Text txt;
    9.  
    10.     void Awake()
    11.     {
    12.         txt = GetComponent<Text>();
    13.     }
    14.  
    15.     void Start ()
    16.     {
    17.         DOTween.Init();
    18.         txt.DOFade(0, 1).SetLoops(-1, LoopType.Yoyo);
    19.     }
    20. }
    21.  
     
    Last edited: Jun 23, 2015