• 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 convictcartel · Jan 12, 2014 at 05:49 AM · varcooldownsubtracting

Cooldown Timer Help!

Ive got my timer working in a sloppy way... I was trying to do something like this:

 var stinkcool = false;
 var stinktimer = 19;
 function stinkcooldowntime () {
 stinkcool = true;
 if (stinkcool == true && stinktimer > 1) {
 stinktimer = stinktimer -1;
 }
 if (stinktimer == 0) {
 stinkcool =false;
 }

but it doesnt keep subtracting, it stops.. any help??

Comment
Add comment · Show 1
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 ankush_Kushwaha · Jan 12, 2014 at 06:37 AM 0
Share

how you are calling function stinkcooldowntime () my guess is that your function stinkcooldowntime () is being called at once (not in a loop) thats why it is not working.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by jhart08 · Jan 12, 2014 at 07:41 AM

If you don't want this in your Update function, then you could do:

 function StinkCooldownTime() {
     stinkCool = true;
     var timer : float = stinkTimer;
     while(timer > 0)
     {
         timer -= Time.deltaTime; //time between this frame and the last frame
         yield WaitForEndOfFrame(); //one of a few YieldInstructions that come in handy
     }
     stinkCool = false;
 }
Comment
Add comment · Show 3 · 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 convictcartel · Jan 12, 2014 at 11:30 AM 0
Share

it still sticks at 18 and doesnt move :/

avatar image jhart08 · Jan 12, 2014 at 06:24 PM 0
Share

Oh sorry, I forgot to mention that the above function is a Coroutine. When you call it, it needs to be as StartCoroutine(StinkCooldownTime());

http://docs.unity3d.com/Documentation/ScriptReference/$$anonymous$$onoBehaviour.StartCoroutine.html for an example.

avatar image convictcartel · Jan 12, 2014 at 10:45 PM 0
Share

I still cant get it to work properly..

avatar image
0

Answer by Nick4 · Jan 12, 2014 at 06:37 AM

It's that when stinktimer gets the integer value '1', it gets stuck. Both if statements don't take it because one of them requires stinktimer to be more than 1 and other requires 0. I'd recommend "for" or "while" though.

Anyway, your code should be like this :

 var stinkcool = false;
 var stinktimer = 19;
 function stinkcooldowntime () {
 stinkcool = true;
 if (stinkcool == true && stinktimer >= 0) {
 stinktimer = stinktimer -1;
 }
 if (stinktimer == 0) {
 stinkcool =false;
 }

With this. When it reaches 0, it will stop substracting and go to second if statement. Good luck.

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 convictcartel · Jan 12, 2014 at 11:30 AM 0
Share

it still sticks at 18 and doesnt move :/

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

21 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

Related Questions

how can i substract one axis by 180? 2 Answers

Is it possible to save a transform.position in a single variable? 1 Answer

var TheCollision : Collision 1 Answer

Best way to share a variable 1 Answer

Script that stops other script from working 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