Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 BarkShark · May 23, 2011 at 06:36 PM · timercountdown

Timer counts down unwanted

Hello,

I'm creating a simple platformer game. In my game you can purchase add-ons. But i want to limit the time you can use these add-ons.

The probem is when i hit the button 'equipment' the timer looks like it's already counting downwards even if i didn't pressed the button.

For example: My begin time is 15 and my end time is 0.

I start the game for 5 seconds. Then i press the button. Normally the time should count downwards starting at 15. But that doesn't happens. It starts and 10 and keeps counting downwards until 0.

Here are my scripts:

In my basic player script I have this:

 static var GJUnlocked = false;
 
 // GJ just means gravity jammer. That's one of the add-ons.
 
 function Update()
 {
 if(Input.GetButtonDown("Equipment"))
   {
   GJUnlocked = true;
   }
 }


And this is my timer script attached to a gui text:

 // eq means equipment. It's just a name i gave the variable.
 
 var eqEndTime : float;
 var eqBeginTime : float;
 static var timeLeft :int;
 
 function Start()
 {
   
     eqEndTime = Time.time + eqBeginTime;
     guiText.text = eqBeginTime +"";
     
 }
 
 function Update()
 {
 
 if(MoveAround.GJUnlocked == true)
     {
     
         timeLeft = eqEndTime - Time.time;
         if (timeLeft < 0) timeLeft = 0;
         guiText.text = "Time left: "+timeLeft;
     }
 }
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 Henrique Vilela · May 23, 2011 at 07:52 PM

You 're setting the eqEndTime variable when your script starts. At this point your time is aways 0, so eqEndTime is aways 15 (assuming that's the value of eqBeginTime). You should set the variable only at the moment you purchase the add-on.

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 BarkShark · May 24, 2011 at 03:47 PM 0
Share

Thank you so much! This is just what i was looking for! Awesome!

avatar image
0

Answer by Antony-Blackett · May 23, 2011 at 07:44 PM

Time.time is the time since your game launched. So even when your object is not unlocked Time.time is still progressing. Try setting timeLeft once in the Start function and then inside your if statement in the Update function use delta time instead. Something like:

 timeLeft -= Time.deltaTime;
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 BarkShark · May 24, 2011 at 02:59 PM 0
Share

This doesn't works, Time.deltaTime is the time in seconds it took to complete the last frame. When i use this the time goes extremely fast down to 0. Do you have any other ideas?

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

3 People are following this question.

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 Through a Label? 1 Answer

Countdown to start level 1 Answer

end game condition 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