- Home /
Countdown remove gui.box
I've searched google a few times for this with no luck but I want to delete a certain countdown GUI.Box after 5 seconds. But i cant find a way to remove a particular GUI.Box. Here is my code. i want the if (Ball_Movement.playerDead == true) GUI.Box(new Rect(180, 400, 100, 20), ""+respawnCountDown); to be deleted after 5 seconds. Also how do i force the textbox to read it as a int? right now that timer is showing up in my scoreboard script as a float countdown, i want it to be 1 2 3 4 5.
using UnityEngine;
using System.Collections;
public class ScoreBoard : MonoBehaviour { // made your score static (changed it to score rather than scorboard global ur call) //public static ScoreBoard instance; public static ScoreBoard global; public static float Score = 0f; public static float Lives = 3f; public static float respawnCountDown = 0; public static float Timer = 0; // Use this for initialization void Start () { //global=this; }
// Update is called once per frame
void Update () {
if (Score >550) // should be 550
{
Application.LoadLevel("YouWon");
}
if (Timer == 5f)
{
// delete the particular gui box here
}
}
// Makes a GUI from the variable score which needs to be tied to when a block dies.
void OnGUI()
{
GUI.Box(new Rect(10, 0, 100, 20), "" + Score);
GUI.Box(new Rect(350, 430, 100, 20), "" + Lives);
if (Ball_Movement.playerDead == true)
{
Wait();
GUI.Box(new Rect(180, 400, 100, 20), ""+respawnCountDown);
}
}
void Wait()
{
respawnCountDown = Timer++ * Time.deltaTime;
}
}
You need to learn about the "Invoke" command, check it out
Answer by kolban · Jun 04, 2012 at 01:07 AM
The following script should achieve what you are looking for.
private var countDown:int = -1;
private var nextEvent:float;
function StartCountDown()
{
countDown = 5;
nextEvent = Time.time + 1;
}
function OnGUI()
{
if (countDown >= 0)
{
GUI.Box(new Rect(100,400, 100, 20), "" + countDown);
if (Time.time > nextEvent)
{
nextEvent = Time.time + 1;
countDown--;
}
}
if (GUI.Button(new Rect(100, 450, 100, 20), "Start"))
{
StartCountDown();
}
}
It contains a button called "Start" that, when pressed, calls "StartCountDown". This starts the count down and while the count down is valid, shows the box containing the countdown.
(Oh ... I coded this up and this is a cut/paste from the working script).
Answer by GuyTidhar · Jun 03, 2012 at 01:29 PM
Here's the relevant code:
void OnGUI()
{
if ( countdown )
GUI.Box(new Rect(180, 400, 100, 20), ((int)respawnCountDown).ToString());
}
// Just call this when ever you want to show the countdown
public void ShowCountdown()
{
StartCoroutine(Countdown());
}
private IEnumerator Countdown()
{
countdown = true;
respawnCountDown = 5;
while ( respawnCountDown > 0 )
{
respawnCountDown -= Time.deltaTime;
yield return null;
}
respawnCountDown = 0;
countdown = false;
}
That doesn't delete the GUI.Box it just blinks then pops back up, also I don't know how to do my score counter to go into that gui.box that way, and last i need to know how to turn a float into an int at least in representation in the box.
Why is respawnCountDown a float if you want to show an int?
Anyways, I placed a cast and a ToString() to give the GUI.Box the string (updated the code).
Once the contdown is down, you should be doing something... You have written that: "Ball_$$anonymous$$ovement.playerDead" is what triggers your countdown. So Once it is false, countdown should be over. Since it is not false once countdown is started, you keep getting the GUI.Box again.
I would have fired the countdown either by a function, and not by waiting for "Ball_$$anonymous$$ovement.playerDead" to be true, or use an event/delegate.
I'll add such a function in the code.
Your answer
![](https://koobas.hobune.stream/wayback/20220613072303im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
full screen wide GUI.Box 3 Answers
Editing certain words in a single NGUI Text Box 0 Answers
How to render a colored 2D rectangle. 6 Answers
GUIBox fontSize without changing other labels? 3 Answers
OnGUI Labels affect different boxes? 1 Answer