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.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class FadeInFadeOut : MonoBehaviour {
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
}
void FadeIn()
{
while (gameObject.GetComponent<Text>().color.a < 0.99f)
{
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));
Debug.Log("incrementing alpha");
}
gameObject.GetComponent<Text>().color = new Color(gameObject.GetComponent<Text>().color.r, gameObject.GetComponent<Text>().color.g, gameObject.GetComponent<Text>().color.b, 1.0f);
Debug.Log("alpha = 1");
}
void FadeOut()
{
while (gameObject.GetComponent<Text>().color.a > 0.01f)
{
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));
Debug.Log("decrementing alpha");
}
gameObject.GetComponent<Text>().color = new Color(gameObject.GetComponent<Text>().color.r, gameObject.GetComponent<Text>().color.g, gameObject.GetComponent<Text>().color.b, 0.0f);
Debug.Log("alpha = 0");
}
}
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
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.
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?
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
@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.
@WheresMommy 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.
IEnumerator LoseGUI()
{
yield return new WaitForSeconds(2.3f);
//Debug.Log("Entered LoseGUI and attempting to play sounds");
//GetComponent<AudioSource>().clip = loseClip;
//GetComponent<AudioSource>().Play();
//Time.timeScale = 0.0f;
//GUImode = "gaming";
UIPanelLose.SetActive(true);
}
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.
or you can follow @WheresMommy suggestion and go with an animator approach and have it run off “realtime” and not “simulation time” (or whatever its called, think that’s in the updatemode in the animator’s inspector)
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using DG.Tweening;
public class BlinkingText : MonoBehaviour
{
Text txt;
void Awake()
{
txt = GetComponent<Text>();
}
void Start ()
{
DOTween.Init();
txt.DOFade(0, 1).SetLoops(-1, LoopType.Yoyo);
}
}