- Home /
Need help with GUI text !
So I've got a script that change the GUI.text after a time period and I want to make it slowly disappear before the next one appear and not just change but I have no idea how to do that , can anyone help me ?
there is my script :
using UnityEngine;
using System.Collections;
public class untrotext : MonoBehaviour {
void Start () {
StartCoroutine(showStuff());
}
// A predone set of messages over time
IEnumerator showStuff() {
transform.guiText.text="Firing...";
yield return new WaitForSeconds(2);
transform.guiText.text="Missile is fired";
// put some text in second line:
transform.Find("lineTwo").guiText.text="Woe to them";
yield return new WaitForSeconds(2);
transform.guiText.text="";
transform.Find("lineTwo").guiText.text="";
}
// someone else can call this for any 1-time message:
public IEnumerator flashMessage(string msg, float len) {
transform.guiText.text=msg;
yield return new WaitForSeconds(len);
transform.Find("lineTwo").guiText.text="l";
}
}
Thank you for your time.
Answer by Lockstep · Mar 28, 2013 at 10:26 PM
The best way to create a fade effect, is to manipulate the alpha channel of the color of the thing you want to fade. In case of a guitext you need to make sure there is a material attached. The font material can easily be found as a child of the font in the project panel. For the scripting part: You need a function that changes the alpha value of the color over time. Something like this:
public float fadeTime = 0.5f; //How long do we fade
IEnumerator fadeText (bool fadeIn){
float startTime = Time.time;
Color c = guitext.material.color;
Color c_invis = new Color(c.r, c.g, c.b , 0f);
Color c_vis = new Color(c.r, c.g, c.b , 1f);
if(fadeIn){
while(Time.time - startTime < fadeTime){ //Loop for fadeTime seconds
guitext.material.color = Color.Lerp(c_invis, c_vis, (Time.time - startTime) / fadeTime);
yield return WaitForEndOfFrame();
}
} else {
while(Time.time - startTime < fadeTime){ //Loop for fadeTime seconds
guitext.material.color = Color.Lerp(c_vis, c_invis, (Time.time - startTime) / fadeTime);
yield return WaitForEndOfFrame();
}
}
}
Thanks the script is really useful but how do I combine those two script ? when I try to use the fade in function on my first script I get an error that it cant be used in this context ...
It is a IEnumerator method, so it called using
StartCoroutine(fadeText(true));
$$anonymous$$ake sure the StartCoroutine is in a piece of code that does not get called each frame:
if(fadeIn){
StartCoroutine(fadeText(true));
fadeIn = false;
}
What fafase is saying is right. You must call it with StartCoroutine and have only one instance of it running at a time. If you need more flexibility, then you could introduce a variable which keeps track of the alpha value. $$anonymous$$aybe like this:
pulic float fadeTime = 0.5f;
public bool fadeIn = false;
private float alphaCounter = 0f;
private Color c_invis = new Color(c.r, c.g, c.b , 0f);
private Color c_vis = new Color(c.r, c.g, c.b , 1f);
void Start(){
Color c = guitext.material.color;
c_invis = new Color(c.r, c.g, c.b , 0f);
c_vis = new Color(c.r, c.g, c.b , 1f);
}
void Update(){
ChangeAlpha();
}
void ChangeAlpha(){
if(fadeIn) alphaCounter += Time.deltaTime / fadeTime;
else alphaCounter -= Time.deltaTime / fadeTime;
alphaCounter = $$anonymous$$athf.Clamp01(alphaCounter);
guitext.material.color = Color.Lerp(c_invis, c_vis, alphaCounter);
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Multiple Weapon Animation Help 0 Answers
Object Disappears on Start 0 Answers
Light disappears by flashlight 1 Answer