Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by cj31387 · Jun 03, 2012 at 11:11 AM · guiboxcountdown

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;        
 }

}

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Fattie · Jun 03, 2012 at 02:12 PM 0
Share

You need to learn about the "Invoke" command, check it out

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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).

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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;
 }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image cj31387 · Jun 03, 2012 at 11:25 PM 0
Share

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.

avatar image GuyTidhar · Jun 04, 2012 at 05:25 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges