Display a Text/GUI on screen when triggerd with Fadein/Fadeout

Hey im new to unity scripting and i need help please.

i got this script

    #pragma strict
    var message : String = "Change Me";
    var displayMessage : boolean = false;
    var displayTime : float = 3.0;
    function OnTriggerEnter ()
    displayMessage = true;
    yield WaitForSeconds (displayTime );
    displayMessage = false;
    function OnGUI ( )
    if ( displayMessage )
    GUI.Label(new Rect(Screen.width * 0.5f - 50f, Screen.height * 0.5f - 10f, 100f, 20f), message);

How can i add a Size for text in that? and fade in and fadeout feature. Thanks!

Unity GUI is a little complicated if you’re new to scripting, so take your time learning it. It will eventually all make sense. :slight_smile:

For size, you’ll probably want to use a GUIStyle, and set the fontSize.

See: Unity - Scripting API: GUIStyle

For fading, you’ll want to set the text color’s alpha value. For example, with a regular label, this is GUIStyle.normal.textColor.

The example below draws the text at font size 20, with 50% transparency.

    var alpha : float = 0.5;
    var style : GUIStyle;
    style = GUI.skin.label;
    style.fontSize = 20; // Set size of text to 20.
    style.normal.textColor = new (1, 1, 1, alpha); // Set color to white w/alpha.
    GUI.Label(new Rect(Screen.width * 0.5f - 50f, Screen.height * 0.5f - 10f, 
        100f, 20f), message, style);

(Warning: untested code above, plus my apologies in case I accidentally snuck some C# code in there out of habit.)

I recommend using a coroutine to handle fading in and out. A coroutine is a function that runs semi-independently from the main loop.

See: http://docs.unity3d.com/Documentation/ScriptReference/index.Coroutines_26_Yield.html

When fading in, the coroutine can change the value of alpha from 0 (completely transparent) to 1 (completely solid). Then OnGUI() can draw the label using the current alpha value.

function FadeIn(duration : float) {
    if (duration > 0) {
        var startTime = Time.time;
        var endTime = start + duration;
        while (Time.time < endTime) {
            var elapsed = Time.time - startTime;
            alpha = (elapsed / duration);
    alpha = 1;