How can I make a GUI Label disappear after a certain amount of time?
In my case I want the text to disappear a couple of seconds after I press space, I've looked at a bunch of solutions but I can't figure it out.
using UnityEngine; using System.Collections;
public class GUI_Label : MonoBehaviour {
 public Rect position = new Rect(Screen.width / 2, Screen.height / 2, 100, 100);
 public string text ="";
 public GUIStyle style = null;
private void Start() {}
 private void OnGUI()
 {
    if (Input.GetKey(KeyCode.Space))
     {       
         GUI.Label(position, text, style);
     }
 }
}
Answer by ellioman · Sep 16, 2016 at 08:00 PM
Here are two different approaches...
Using states...
 using UnityEngine;
 using System.Collections;
 
 public class GUI_Label : MonoBehaviour
 {
     public Rect position = new Rect(Screen.width / 2, Screen.height / 2, 100, 100);
     public string text ="";
     public GUIStyle style = null;
 
     // Variables for making the text disappear
     public float hideTextDuration;
     private float startTime = 0f;
     private TextDisplayState textState = TextDisplayState.Showing;
     private enum TextDisplayState
     {
         Showing,
         Disappearing,
         NotShowing
     };
 
     // Update is called once per frame
     private void Update()
     {
         if (textState == TextDisplayState.Showing)
         {
             if (Input.GetKeyUp(KeyCode.Space))
             {
                 startTime = Time.time;
                 textState = TextDisplayState.Disappearing;
             }
         }
         else if (textState == TextDisplayState.Disappearing)
         {
             if (Time.time - startTime > hideTextDuration)
             {
                 textState = TextDisplayState.NotShowing;
             }
         }
     }
 
     // Called (several times per frame) for rendering and handling GUI events.
     private void OnGUI()
     {
         if (textState != TextDisplayState.NotShowing)
         {
             GUI.Label(position, text, style);
         }
     }
 }
 
Using coroutines...
 using UnityEngine;
 using System.Collections;
 
 public class GUI_Label : MonoBehaviour
 {
     public Rect position = new Rect(Screen.width / 2, Screen.height / 2, 100, 100);
     public string text ="";
     public GUIStyle style = null;
 
     // Variables for making the text disappear
     public float hideTextDuration;
     private bool shouldCheckInput = true;
     private bool shouldShowText = true;
 
     // Update is called once per frame
     private void Update()
     {
         if (shouldCheckInput)
         {
             if (Input.GetKeyUp(KeyCode.Space))
             {
                 shouldShowText = true;
                 shouldCheckInput = false;
                 StartCoroutine(WaitAndMakeTextDisappear(hideTextDuration));
             }
         }
     }
 
     private IEnumerator WaitAndMakeTextDisappear(float waitTimeInSeconds)
     {
         yield return new WaitForSeconds(waitTimeInSeconds);
         shouldShowText = false;
     }
 
     // Called (several times per frame) for rendering and handling GUI events.
     private void OnGUI()
     {
         if (shouldShowText)
         {
             GUI.Label(position, text, style);
         }
     }
 }
 
Answer by GucciTransit · Sep 19, 2016 at 01:31 AM
@ellioman Thanks! The coroutines method worked after I added "shouldShowText = true;" to the if input section. I couldn't get the states method to work :/ Here's the final result: using UnityEngine; using System.Collections;
public class Memes_GUI : MonoBehaviour { public Rect position = new Rect(Screen.width / 2, Screen.height / 2, 100, 100); public string text = ""; public GUIStyle style = null;
 // Variables for making the text disappear
 public float hideTextDuration;
 private bool shouldCheckInput = true;
 private bool shouldShowText = false;
 // Update is called once per frame
 private void Update()
 {
     if (shouldCheckInput)
     {
         if (Input.GetKeyUp(KeyCode.Space))
         {
             shouldCheckInput = false;
             shouldShowText = true;
             StartCoroutine(WaitAndMakeTextDisappear(hideTextDuration));
         }
     }
 }
 private IEnumerator WaitAndMakeTextDisappear(float waitTimeInSeconds)
 {
     yield return new WaitForSeconds(2);
     shouldShowText = false;
 }
 // Called (several times per frame) for rendering and handling GUI events.
 private void OnGUI()
 {
     if (shouldShowText)
     {
         GUI.Label(position, text, style);
     }
 }
}
Awesome! Great that it's working for you. I've fixed my answer to solve the issue you pointed out. Please click "Accept" on my answer so that this case can resolved :)
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                