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 MattBee · Mar 31, 2014 at 10:01 PM · coroutinelabeldisappear

Make label disappear after 1 second?

Hi, so I'm making a car game for my university project and I'm trying to implement a countdown timer at the begging of the game which will countdown from 3 and tell the player to go.

I have it working as it should, the player and AI can't move until the timer reaches 0 but the problem is that I'm using labels to display "3", "2", "1" and "GO!" when it should be displayed but the labels don't disappear and just stack. I've tried a coroutine and using a boolean but neither worked. Here's the code from the OnGUI method:

 using UnityEngine;
 using System.Collections;
 
 public class StartCountdownScript : MonoBehaviour 
 {
 
     public float countdownTime = 7.0f;
     public float resetPlayerTorque;
     public float resetAITorque;
 
     public GameObject[] aiCars;
     public GameObject player;
 
     public GUIStyle customGUIStyle;
 
     public int labelWidth = 250;
     public int labelHeight = 250;
 
     public bool toggleLabel = false;
 
 
     void OnGUI()
     {
         if (countdownTime <= 5)
         {
              GUI.Label(new Rect(Screen.width/2 - labelWidth/2, Screen.height/2 - labelHeight/2, labelWidth, labelHeight), "3", customGUIStyle);
         }
 
         if (countdownTime <= 3)
         {
             GUI.Label(new Rect(Screen.width/2 - labelWidth/2, Screen.height/2 - labelHeight/2, labelWidth, labelHeight), "2", customGUIStyle);
         }
 
         if (countdownTime <= 1)
         {
             GUI.Label(new Rect(Screen.width/2 - labelWidth/2, Screen.height/2 - labelHeight/2, labelWidth, labelHeight), "1", customGUIStyle);    
         }
 
         if (countdownTime <= 0)
         {
             GUI.Label(new Rect(Screen.width/2 - labelWidth/2, Screen.height/2 - labelHeight/2, labelWidth, labelHeight), "GO!", customGUIStyle);
         }
     }

Can someone tell me how to go about displaying each label for just 1 second then disappearing? I'm sure I'm overlooking something so simple or have made a silly mistake.

All help is appreciated, Thanks ;)

Comment
Add comment
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

2 Replies

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

Answer by Lo0NuhtiK · Mar 31, 2014 at 10:11 PM

They're stacking because 0

Ex :

     float time = 7f ;
     string content = "" ;
     bool countDownDone = false ;
 
     void Update()
     {
         //decrement time
         time -= Time.deltaTime ;
     }
 
 
     void OnGUI()
     {
         CountDown() ;
     }
 
     void CountDown()
     {
         //early-out of this function if we've already done the countdown
         if(countDownDone) return ;
 
         int t = Mathf.FloorToInt(time) ;
         switch(t)
         {
             case 4 :
                 content = "3" ;
                 break ;
             case 3 :
                 content = "2" ;
                 break ;
             case 2 :
                 content = "1" ;
                 break ;
             case 1 :
                 content = "GO!!" ;
                 break ;
             case 0 :
                 countDownDone = true ;
                 break ;
         }
 
         GUI.Label(--yourRect--, content, --yourStyle) ;
     }
Comment
Add comment · Show 5 · 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 T27M · Mar 31, 2014 at 10:26 PM 0
Share

Deleted my answer since yours is more detailed. $$anonymous$$ine worked, but it depends on how you decrement the timer value.

avatar image Lo0NuhtiK · Mar 31, 2014 at 10:30 PM 0
Share

Damn it lmao .. I just posted a comment on your answer.. ninja'd me again

---Yeah, I guess it would depend on that. But assu$$anonymous$$g that they decrease T by deltaTime in Update() and don't round it to an int like I did, then (==) would be lucky to ever even hit "1" because T could be 1.023 this update, and then be 0.283899 the next one, etc.

avatar image T27M · Mar 31, 2014 at 10:33 PM 1
Share

I called InvokeRepeating() on a method at Start() at a 1 second interval, method simply decremented the timer. $$anonymous$$aybe should have included that. :)

avatar image MattBee · Apr 01, 2014 at 07:53 AM 0
Share

Thanks for the detailed answer. I completely overlooked that the if statement is always true.

Thanks, I'll give this a shot later on today.

avatar image MattBee · Apr 01, 2014 at 06:00 PM 1
Share

So I put this together, in place of all my if statements and it works like a charm.

Thank you very much.

avatar image
0

Answer by oktemre · Mar 31, 2014 at 10:14 PM

Where do you decrease your countdownTime variable? I suggest adding a line to the update function as below;

 countdownTime -= Time.deltaTime;

also don't make seperate if statements and use else if.

Comment
Add comment · Show 1 · 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 MattBee · Apr 01, 2014 at 07:52 AM 0
Share

I have that, I must have forgotten to copy it in.

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

23 People are following this question.

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

Related Questions

Make a platform disappear after the next one appear 2 Answers

How to display a GUI label for a certain amount of time 2 Answers

Singletons with coroutines? 4 Answers

Call long running AI function without tying up game loop? 1 Answer

Coroutine and terrain 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