make GUI texture disappear after a few second ?

Hi i have a problem with a script.
i made this:

var toggleGUI : boolean;
var textureToDisplay : Texture2D;
    function OnTriggerEnter (other : Collider) {
    toggleGUI = true;
    function OnTriggerExit (other : Collider) {
    toggleGUI = false;
    function OnGUI () {
    if (toggleGUI == true)
    GUI.Label (Rect (500, 200, textureToDisplay.width, textureToDisplay.height),

It display a texture when I’m in a trigger but i want the texture disappear (not destroy) after a few second (like 3sec) but don’t know how to do this =s

Tank you in adavance :wink:

Well, first off, since toggleGUI is already a boolean, you do not need to compare it to true or false in OnGUI. Instead, you only need to put:


That aside, we come down to your issue. Instead of simply toggling toggleGUI, you can use

yield WaitForSeconds(3.0);
toggleGUI = false;

This way, you will pause the function until it is toggled.


If you want the message to only display for 3 seconds, put the above script just after you set toggleGUI to true. And remove the OnTriggerExit

For more information, check out the documentation here