- Home /
gui label on/off with timer
I'm guessing it's a simple issue, I'm basically trying to show a GUI label for only 5 seconds then disappear, works but, once you replace the errorLabel variable with something else then timer doesnt get renewed for the full 5 seconds and once its off it cant be turned on for also 5 seconds or so it seems, any ideas?
void OnGUI ()
{
if(errorLabel != "")
{
StartCoroutine(RemoveLabel());
}
}
IEnumerator RemoveLabel()
{
yield return new WaitForSeconds (5);
errorLabel = "";
}
Answer by zharik86 · Nov 14, 2014 at 07:34 AM
You start many Coroutine each frame, so far variable "errorLabel" is not equal "". And better, transfer code in Update(). I write simple example:
private string errorLabel = "Error";
private bool isCor = false; //for Coroutine check start/stop
void Update() {
if(errorLabel != "" && !isCor) {
StartCoroutine(RemoveLabel());
}
}
IEnumerator RemoveLabel() {
isCor = true;
yield return new WaitForSeconds (5);
errorLabel = "";
isCor = false;
}
void OnGUI() {
//Show error
if (errorLabel != "") {
GUI.Label(new Rect(0, 0, 150, 70), errorLabel);
}
}
I hope that it will help you.
thanks, i see, whats the difference in using the check in the update rather than the OnGUI though? performance reason?
@leonalchemist Yes, performance reason. As the function OnGUI() can be executed several times for a frame.
Your answer
Follow this Question
Related Questions
How Would I Make A GUI Label Fade After A Certain Amount Of Time? 1 Answer
Timer Between Labels 2 Answers
How to set text(timer) to a fixed place 1 Answer
Creating time elapsed label, in js? 1 Answer
Timer Pause 1 Answer