- Home /
Fading GUI text
Ive been looking around and from different posts I've put together a script that has a GUI text in the middle of the screen for certain amount of time. However it doesn't seem to work properly. The GUI text is there but its very small and its not in one line, also it fails to fade away. could someone find where I've gone wrong. Thanks var color : Color;
function Start()
{
color = Color.white;
yield FadeOutAfterTime(5);
}
function OnGUI () {
var centeredStyle = GUI.skin.GetStyle("Label");
centeredStyle.alignment = TextAnchor.UpperCenter;
GUI.Label (Rect (Screen.width/2-50, Screen.height/2-25, 100, 50), "Level 1", centeredStyle);
}
function FadeOutAfterTime(time : float)
{
yield WaitForSeconds(5);
yield Fade();
}
function Fade()
{
while (color.a > 0)
{
color.a -= Time.deltaTime;
yield;
}
}
Answer by Griffo · Mar 17, 2013 at 08:42 PM
Add a GUI Text to your scene, make a new script, put this in -
#pragma strict
var color : Color;
function Start(){
color = Color.white;
}
function Update(){
Fade();
}
function Fade(){
while (guiText.material.color.a > 0){
guiText.material.color.a -= 0.1 * Time.deltaTime * 0.05;
yield;
}
}
In the inspector set to what you want, like below, using another font like I have to get a better and bigger effect -
Thats just the basis of the fade effect for the GUI Text, call and use it as you wish like in you question.
Hope that helps you out, I've got to leave my computer now for some time so I wont be able to help for sometime but I'm sure someone here will help you out ..
Sorry just noticed I put guiText and you cant use that, I've edited the script, should be O$$anonymous$$ now.
Right lets start again ..
Create an empty game object, name it whatever you want, add a box collider and resize to around 5.5.5 and tick is Triggered in the inspector, then add this script -
#pragma strict
private var someText : GameObject;
function Awake(){
someText = GameObject.Find("GUItext");
someText.gameObject.SetActive(false);
}
function OnTriggerEnter (other : Collider) {
if(other.gameObject.tag == "Player"){
someText.gameObject.SetActive(true);
yield WaitForSeconds(10.0); // Wait for X seconds then turn off the gameObject
someText.gameObject.SetActive(false);
}
}
Now add GUI Text to the scene, rename it "GUItext" and add this script -
#pragma strict
var color : Color;
function Start(){
color = Color.white;
Fade();
}
function Fade(){
while (guiText.material.color.a > 0){
guiText.material.color.a -= 0.1 * Time.deltaTime * 2;
yield;
}
Destroy (gameObject);
}
In the inspector set the GUItext as the picture I posted above,make sure whatever collides with the box collider in the first script above has the tag name of Player or change it in the script to whatever it's tag is, I've tested this and it works so good luck in following these instructions.
Answer by michaelb212 · Jul 13, 2015 at 06:32 AM
Update this answer to unity 5:
Since color is a struct, the line:
material.color.a -= 0.1 * Time.deltaTime * 2;
will be a compiler error.
The right way to do it would be saving the color to a local variable, or better using Color.operator-
material.color -= new Color(0, 0, 0, 0.1 * Time.deltaTime * 2);
But the best way in my opinion is using Color.Lerp
public float FadeoutTime;
private Color startColor;
private Color endColor;
void Start()
{
startColor = GetComponent<Text>().color;
endColor = startColor - new Color(0, 0, 0, 1.0f);
}
void Update ()
{
GetComponent<Text>().color = Color.Lerp(startColor, endColor, FadeoutTime)
}