GUI.Label fading problem

Hello !
My current code is working nice and shiny. Only problem is that when my label text are fading, my whole gui fades ! That mean’s crosshair… healt bars… etc

How i can fade only this one label ?

var textWidth : int = Variables.NotifyText.Length * 7;
GUI.Label(new Rect(Screen.width/2 - textWidth/2, Screen.height/2 + 150, textWidth * 7, 20), "" + Variables.NotifyText);

TempTextTime -= Time.deltaTime;
if(TempTextTime < 3){
color.a -= Time.deltaTime * 0.35;
}

if (TempTextTime < 0){
color = Color.white;
TempTextTime = TextFadeOffTime; // clamp the timer to zero and stop text showing
Variables.DrawNotifyText = !Variables.DrawNotifyText;
Variables.NotifyText = "";
}

I dont see the usage of color, but you may need to reset the color after you used it for your label, so that the other UI elements may use the original unfaded value.

+1
GUI.color is relative where you’re writing it, which means:

GUI.color = Color.red;
GUILayout.Label("Label1");
GUI.color = Color.white;
GUILayout.Label("Label2");

The first label will be red, the second one white.

This decreases alpha channel:

TempTextTime -= Time.deltaTime;
if(TempTextTime < 3){
color.a -= Time.deltaTime * 0.35;
}

and this will return old alpha channel value after fading:

if (TempTextTime < 0){
color = Color.white;
TempTextTime = TextFadeOffTime; // clamp the timer to zero and stop text showing
Variables.DrawNotifyText = !Variables.DrawNotifyText;
Variables.NotifyText = "";
}

It works, i don’t want that you think it won’t work !! :slight_smile: Problem is that all my GUI fades when i am decreasing color.a -= Time.deltaTime * 0.35; and that’s annoying… i only want to decrease this one specific label GUI.Label(new Rect(Screen.width/2 - textWidth/2, Screen.height/2 + 150, textWidth * 7, 20), “” + Variables.NotifyText);
alpha channel

Your code is right, try to reset the color by using GUI.color = Color.white after the Label line.

You mean this ? Not working :frowning:

var textWidth : int = Variables.NotifyText.Length * 7;
GUI.Label(new Rect(Screen.width/2 - textWidth/2, Screen.height/2 + 150, textWidth * 7, 20), "" + Variables.NotifyText);
GUI.color = Color.white;
TempTextTime -= Time.deltaTime;
if(TempTextTime < 3){
color.a -= Time.deltaTime * 0.35;
}

if (TempTextTime < 0){
color = Color.white;
TempTextTime = TextFadeOffTime; // clamp the timer to zero and stop text showing
Variables.DrawNotifyText = !Variables.DrawNotifyText;
Variables.NotifyText = "";
}

}

GUI.color affects the whole GUI.

Instead, use a GUIStyle for your label, and change the alpha only of that GUIStyle’s color.

Do this.

.......
GUI.color = color;
GUI.Label(new Rect(.......);
GUI.color = Color.white;
.......

and take out any other lines that have GUI.color = color

Can i have little example ?

hpjohn’s solution will also work. But if you don’t want to deal with GUI.color, you could do something like this (pseudocode):

// One-time setup:
var myGuiStyle = new GUIStyle(GUI.label);

// Loop:
myGuiStyle.normal.textColor.a -= Time.deltaTime * 0.35;
GUI.Label(rect, text, myGuiStyle);

Hmm that is some nice line of code ! But unfortunaly im getting error:
Assets/Scripts/GuiDraw.js(9,22): BCE0024: The type ‘UnityEngine.GUIStyle’ does not have a visible constructor that matches the argument list ‘(function(UnityEngine.Rect, String): void)’.

and unity points var myGuiStyle = new GUIStyle(GUI.Label);

Im totally out of GUI stuff on unity , i hope you still can help me :slight_smile:

EDIT. It works now, thank you !

You need to reset color.a to full value of fading it in the code.