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 = "";
}
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.