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 schwertfisch · Jan 16, 2011 at 05:33 PM · timercountdownextend

Countdown timer trouble?

I have a script that with the use of a GUIText object, displays the time left before the game is over. The time begins from 15 and reduces. If it reaches 0... game over. I have a question regarding this script:

GUITextTimeLeftEM.js

private var startTime; static var timeLeftDisplayed : int; private var timeLeft: float= 15; var textToShow: GUIText;

function Awake() { startTime = Time.time+0.5; }

function OnGUI () { var guiTime = Time.time - startTime; timeLeftDisplayed = timeLeft - (guiTime); if (timeLeftDisplayed == 0) { print ("Time is Over"); LoadNextLevel (); } }

function Update(){ textToShow.text = "Time left: " + timeLeftDisplayed.ToString(); }

function LoadNextLevel () { cubeMovementIVEndlessMode.chainHits =0; cubeMovementIVEndlessMode.frenzyOn=false; yield WaitForSeconds (.5); Application.LoadLevel(Application.loadedLevel + 1); }

I use

startTime = Time.time+0.5; 

because if I just use:

startTime = Time.time;

the timer goes from 15 to 14 in 0.5" instead of the expected 1". Why is that?

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

1 Reply

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

Answer by azzogat · Jan 16, 2011 at 05:50 PM

There's quite a few prettier ways to do what you need. What you're doing will not stick because timeLeftDisplayed will be immediately reset to what it should be on the next ongui refresh.

So immediately after you add 1 second it does what you're telling it to do:

timeLeftDisplayed = 15-(Time.time-(gameStartTime+0.5));

If you want to stick to this way of counting down you should actually do

GUITextTimeLeftEM.timeLeft += 1;

(and of course, don't make the variable private)

You should check this question out for a more correct way of counting down. Also remember that OnGui is a performance killer.

P.S: the first part of the answer was for an edited-out part of the question.

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 schwertfisch · Jan 16, 2011 at 06:42 PM 0
Share

Thanks a lot azzogat :)

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

No one has followed this question yet.

Related Questions

Countdown Timer Help About putting 0's 1 Answer

Timer counts down unwanted 2 Answers

How to stop a timer with GUI button? 1 Answer

Countdown timer in minutes:seconds:milliseconds 2 Answers

Timer not work :( 0 Answers


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