- Home /
making LateUpdate wait for Fade in/out guiText
Hi,
I am trying to display something like a subtitle, the guiText.text changes at certain seconds.
What I am having trouble with is, I can not fade the guiText out - change it - fade it back in. I am pretty sure there is a very simple way to do this, I have looked into yield, coroutines and tried everything I could think of. Here is what I have:
function LateUpdate () {
seconds=GameObject.Find("Timer").GetComponent("Timer").seconds;
switch(seconds){
case 0:
fadeOut();
fadeIn();
print("second 0");
guiText.text="Lascia Chi'o Pianga";
break;
case 6:
print("6th second");
fadeOut();
fadeIn();
guiText.text="Mia Crudo, Sorte";
break;
default:
//guiText.text=guiText.text;
break;
}
and of course the fadeIn and fadeOut functions
function fadeIn(){
guiText.material.color.a=Mathf.Lerp(0,1,Time.time);
yield WaitForSeconds(1);
}
function fadeOut(){ guiText.material.color.a=Mathf.Lerp(1,0,Time.time); yield WaitForSeconds(1);
}
What I was hoping for was, that the yield WaitForSeconds statements would interrupt the LateUpdate. What happens is that, the first line fades in perfectly, stays on screen, then when case 6 is true, the second line kicks in instantly.
So, the first FadeIn works, the rest doesn't? I am not sure what is going on.
Thanks in advance :)
Answer by Eric5h5 · Mar 05, 2010 at 08:26 PM
You can't interrupt Update or LateUpdate at all; they always run every frame. For scheduling a series of events, you are better off forgetting about Update and using coroutines instead. One problem is that Mathf.Lerp uses the range of 0 to 1 for the third parameter, so your fade in and out functions will only work during the first second that Unity is running.
As a side note, GameObject.Find is slow, so doing that in Update is not optimal. It should be done once in Start or Awake, and the result cached. Actually I'm not sure you need the Timer script at all...using the Fade script in the wiki, I would do this:
function Start () { FadeOutIn("Lascia Chi'o Pianga"); yield WaitForSeconds(6.0); FadeOutIn("Mia Crudo, Sorte"); }
function FadeOutIn (s : String) { yield Fade.use.Alpha (guiText.material, 1.0, 0.0, 1.0); guiText.text = s; yield Fade.use.Alpha (guiText.material, 0.0, 1.0, 1.0); }
Thank you Eric5h5, this works great, it saved me a lot of time :) I did not know of this script in the wiki.
Your answer

Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
How do i make a TextArea Descend when messages happen 0 Answers
Need help with appearing text on the trigger 1 Answer
Input a string and the computer answers with a command? 4 Answers
Making Credits Scroll Upwards on Screen 2 Answers