• 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 Persona · Oct 08, 2010 at 05:48 PM · timercounter

Timer Runtime Problem

I'm using a timer that uses time.time to count down from 60 seconds to 0. The problem is that:

A.) I can't add time, like to extend the count down.

B.) It runs even in the main menu.

Can anyone offer suggestions to remedy this problem?

Here's the code for now:

static var Timer: int = 0; static var startTime: int = 120; var timeLeft: int; var prefix = "Time Left: "; var suffix = " ";

function Awake() { Timer = startTime; }

function Update () {

guiText.text = prefix + timeLeft + suffix;

Timer = Time.time * -1;

timeLeft = startTime + Timer;

if(timeLeft <= 0) { Application.LoadLevel ("Game Over"); }

}

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 · Oct 09, 2010 at 02:11 AM

To address the problems you're having, I would manually create a timer, rather than using Time.time.

private var timeRemaining : float = 60.0;

function Update() { timeRemaining -= Time.deltaTime; }

Using this approach:

  • you can subtract time in an Update loop you know will only get called when not on the main menu.
  • you can add to timeRemaining to extend the countdown.

If you'd rather use Time.time, you could use a few other variables to track bonus time and paused time, but I honestly think this way is simpler. Please ask if you have any questions!

Comment
Add comment · Show 8 · 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 Persona · Oct 10, 2010 at 07:48 PM 0
Share

The only problem is that it decreases too fast, not by seconds.

avatar image · Oct 10, 2010 at 10:38 PM 0
Share

Every Update(), the value will decrease by deltaTime, which is the time for one frame. At 60 frames per second, that's 0.0166 seconds. What's the actual problem that you're having?

avatar image Persona · Oct 11, 2010 at 11:46 AM 0
Share

I needed it to decrease at one second intervals.

avatar image · Oct 11, 2010 at 12:04 PM 0
Share

Display the timer as an int and it'll round to the nearest integer. Or use mod 1 (%) to subtract the remainder.

avatar image Persona · Oct 11, 2010 at 01:32 PM 0
Share

No that's still not it. It displays whole numbers, but the rate of decease is too fast.

The timer should decrease at a rate of one number per second, but ins$$anonymous$$d it flies through the numbers, going through 2 $$anonymous$$utes in under 5 seconds.

Show more comments
avatar image
0

Answer by AVividLight · Oct 08, 2010 at 06:18 PM

Can you post your script? We can't help unless we can see what you wrote...

Comment
Add comment · Show 4 · 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 skovacs1 · Oct 08, 2010 at 06:36 PM 2
Share

This isn't an answer to the question. This should probably be a comment on the question.

avatar image AVividLight · Oct 08, 2010 at 08:03 PM 0
Share

Yeah, but I intend to help once I can see his script, so when (or if) he posts the script I plan on editing my post... Although I could have asked him to post his script I don't think it matters... Sorry to confuse you!

-Gibson

avatar image Persona · Oct 11, 2010 at 01:35 PM 1
Share

Posted the code if you want to take a crack at it.

avatar image AVividLight · Oct 11, 2010 at 10:14 PM 0
Share

Hey, thanks... I'll check it out!

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

How to realize accurate time counter (millisecond precision) 3 Answers

GUI Help, timer and counter 1 Answer

player on fire timer 2 Answers

How to add a counter on screen! 2 Answers

How to stop a countdown from counting down 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