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
6
Question by MikezNesh · Jun 21, 2010 at 08:19 AM · timercountdown

Count Down Timer.... (per second)

How would I add a count down timer that counts down per second and displays how many seconds left?

My Current Code: (yes I know that the Update function is incorrect but I'm just testing the decrementing)

var seconds = 60;

function Update () { seconds --; var a = GameObject.Find ("Timer"); var testMesh : TestMesh = a.GetComponent(TextMesh); testMesh.text = seconds.ToString ();

When I used this code.... it went way too fast and went to negatives.

So can someone tell me a professional script (stops at zero and changes per SECOND)? 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

3 Replies

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

Answer by Mike 3 · Jun 21, 2010 at 08:31 AM

Something like this should work:

var endTime : float; var textMesh : TextMesh;

function Start() { endTime = Time.time + 60; textMesh = GameObject.Find ("Timer").GetComponent(TextMesh); textMesh.text = "60"; }

function Update() { var timeLeft : int = endTime - Time.time; if (timeLeft < 0) timeLeft = 0; textMesh.text = timeLeft.ToString(); }

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 MikezNesh · Jun 21, 2010 at 08:43 AM 1
Share

Perfect $$anonymous$$ike. Thanks for answering like 5 of my questions perfectly!

avatar image MikezNesh · Jun 21, 2010 at 08:49 AM 1
Share

$$anonymous$$ight you answer one more? Thanks. The other guy didn't understand what I meant.

avatar image MikezNesh · Jun 21, 2010 at 08:49 AM 1
Share

Here is the link: http://answers.unity3d.com/questions/12971/how-do-you-make-a-fluid-net-like-substance/12977#12977

avatar image Mike 3 · Jun 21, 2010 at 09:10 AM 1
Share

I think $$anonymous$$just did that for you :)

avatar image mayur.bendale · Nov 07, 2012 at 10:16 AM 0
Share

Excellent!!!!!!!

avatar image
15

Answer by Eric5h5 · Jun 21, 2010 at 03:07 PM

Here's another method...it only fires once per second, and stops when done, so it doesn't use any unnecessary CPU cycles:

var seconds = 60; private var textMesh : TextMesh;

function Start () { textMesh = GameObject.Find ("Timer").GetComponent(TextMesh); textMesh.text = seconds.ToString(); InvokeRepeating ("Countdown", 1.0, 1.0); }

function Countdown () { if (--seconds == 0) CancelInvoke ("Countdown"); textMesh.text = seconds.ToString(); }

Comment
Add comment · Show 7 · 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 MikezNesh · Jun 21, 2010 at 11:11 PM 1
Share

Does this make it more efficient?

avatar image MikezNesh · Jun 21, 2010 at 11:20 PM 1
Share

Can you also answer this question? Thanks! http://answers.unity3d.com/questions/13016/stay-in-trigger-range-script

avatar image Eric5h5 · Jun 21, 2010 at 11:49 PM 1
Share

@$$anonymous$$ikezNesh: it only runs once per second ins$$anonymous$$d of every frame, and stops when it's done ins$$anonymous$$d of continuing to run every frame (and uses one less variable), so indeed it's more efficient.

avatar image Jean-Fabre · Nov 01, 2010 at 01:38 PM 1
Share

very good and understandable

avatar image Karsnen_2 · Dec 09, 2011 at 04:13 AM 1
Share

Excellent code done here.

Show more comments
avatar image
2

Answer by heruka · Jul 12, 2015 at 12:00 PM

Here is my adapted solution using Unity 5 and following the video at https://www.youtube.com/watch?v=SbehR3Ad3oU

My solution is in my "GameController" script, so the formatting (position, font, type, etc) are set in the inspector window. Also, the "GameOver" function I call, is inside the "GameController" script. Make sure you've got Using UnityEngine.UI listed at the top. I've also place the "(int)" in front of time remaining because I wanted it to countdown per second vs the timer showing per frame. Hope this helps!

     public Text cdtimerText;
 
     public float timeRemaining;
 
     // Use this for initialization
     void Start () {
         cdtimerText.text = "";
 
     }
     
     void Update ()
     {
         // Functional game countdown timer
         timeRemaining -= Time.deltaTime;
         if (timeRemaining > 0) {
             cdtimerText.text = "TIME: " + (int)timeRemaining;
         } else {
             cdtimerText.text = "TIME'S UP!";
             GameOver ();
 
         }
     }
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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Timer doesn't work properly 2 Answers

When Score goes up by 25 change timer value 1 Answer

Countdown timer trouble? 1 Answer

Countdown to start level 1 Answer

Count down on gameobject 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