Make GUI Text only appear for a certain amount of time?

Hello everyone!
I was just wondering how to make text appear for, let’s say, 2 seconds. My goal is to try to make a Slenderman-like game. So when you click the page (In my case it is an Anti-Biotic) it will say “1/10 Anti-Biotics Retrieved” and then will disappear after 2 seconds. Here is my code:

#pragma strict
@script RequireComponent( AudioSource )

var AntiBiotics : float = 0;
var ABsToWin : int = 10;
var paperSound : AudioClip;
var isCollected : boolean = false;

function Start() {
    AntiBiotics = 0;
}
function OnMouseDown() {
    isCollected = true;
    AntiBiotics += 1;
    audio.PlayOneShot(paperSound); 
    //Destroy(this.gameObject);
}
function OnGUI() {
    if(isCollected == true) {
        for
        GUI.Label (Rect( (Screen.width * 0.5), (Screen.height * 0.5), 50, 25), AntiBiotics + " / " + ABsToWin);
    }
}

Thanks!
-AngrySc0rpi0n

Use the GUI in 4.6; it’s simpler.

function OnMouseDown () {
    // other stuff goes here
    textGO.GetComponent(Text).enabled = true;
    yield WaitForSeconds (2);
    textGO.GetComponent(Text).enabled = false;
}

–Eric

1 Like

When I tried that it didn’t understand what “textGO” and “Text” were. Do I have to define any variables or import any assets?

Thanks
-Harry

textGO is the game object that the Text component would be attached to. Do this tutorial if you haven’t done 4.6 UI stuff before.

–Eric

Thanks :slight_smile: