I’m working on a project in Unity using C# scripting. The GUI.Box will appear at the top the screen. The box will disappear when the player leaves the spot. How can I make the box stay there for an additional 3 seconds after the player leaves the designated spot?
Danpe’s code corrected (working code):
bool shown = false;
void OnGUI () {
if (car.transform.position.y>=43 && car.transform.position.y<=44)
{
shown = true;
}
else if (shown)
{
StartCoroutine(DisapearBoxAfter(3.0f));
}
if(shown)
{
GUI.Box(new Rect((Screen.width/2)-200,0,400,30) , "King of the hill");
}
}
IEnumerator DisapearBoxAfter(float waitTime) {
// suspend execution for waitTime seconds
yield return new WaitForSeconds(waitTime);
shown = false;
}
void Update () {
OnGUI ();
}
1 Answer