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 $$anonymous$$ · Feb 01, 2014 at 01:11 PM · guitimerflashlightbatterytorch

How do you write a Pauseable-Timer Script in Unity JavaScript?

Hey Guys, I wondering how you would be able to make a Pauseable-Timer Unity.

I have a flashlight toggle script here:


 var flashlight : boolean;
 
 function Update () {
     if (Input.GetButtonDown("Fire2")) {
        GetComponent(Light).enabled = !GetComponent(Light).enabled;
     }
 
     if (GetComponent(Light).enabled)
     {
         flashlight = true;
         Debug.Log("Flashlight On");
     }else{
         flashlight = false;
         Debug.Log("Flashlight Off");
 
     }
 } 



I need to make a battery as a form as a GUIBox for the flashlight. At the start of the game, it would start at 100% and drop down to 0% in 1 second intervals. The player can switch off the flashlight which would pause the timer. (`Time.deltaTime`) I know I could use Time.deltaTime but I can't find a way to pause it. (P.S. I also need the player to find battery's scattered along an asylum or house which would increase their battery life in the flashlight.)

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 GTX Titan · Feb 01, 2014 at 01:25 PM 1
Share

Do this: if(flashlight){ timer += Time.deltaTime;} It will only add to the timer when the flashlight is on.

1 Reply

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

Answer by GTX Titan · Feb 01, 2014 at 01:48 PM

Do this:

 if(flashlight){
     if(timer < maxTimer)
         timer += Time.deltaTime;
 }

It will only add to the timer when the flashlight is on. This way you can then reset the timer like this: timer = 0; when a battery is picked or something. Also you would have to make the flashlight battery indicator work with the timer variable... for example:

 maxTimer = 120;
 indicator = (maxTimer - timer)/maxTimer*100;
 print(indicator + "%");

or something like that...

Comment
Add comment · Show 2 · 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 $$anonymous$$ · Feb 02, 2014 at 04:13 PM 0
Share

Im sorry, but i'm not sure how to code that as a whole. Sorry for me being such a n00b. And, if you want to, could you write a battery pickup script. If you don't want to don't. I don't even $$anonymous$$d if you don't write the script anyway. :-D Thank You

avatar image GTX Titan · Feb 02, 2014 at 08:23 PM 0
Share

Well here's somewhat complete script:

 private var maxTimer:int;
 
 function Start(){
 
 maxTimer = 120; // time in seconds for the battery to reach 0% from 100%
 
 }
 
 function Update () {
 
 var indicator:float;
 
 if (Input.GetButtonDown("Fire2")) {
 
 GetComponent(Light).enabled = !GetComponent(Light).enabled;
 
 }
  
 if (GetComponent(Light).enabled)
 {
 
 flashlight = true;
 
 Debug.Log("Flashlight On");
 
 }else{
 
 flashlight = false;
 
 Debug.Log("Flashlight Off");
  
 }
 
 if(flashlight){
 
 if(timer < maxTimer)
 
 timer += Time.deltaTime;
 
 }
 indicator = (maxTimer - timer)/maxTimer*100;
 
 print(indicator + " %"); // this will show up on the bottom of unity left side
 }

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

18 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

Related Questions

Battery Pickup Regeneration 2 Answers

-light.intensity, light.spotAngle and light.color with variable? 0 Answers

How to make a GUI to show how much battery is left on Flashlight? 1 Answer

problem accessing android jar in unity 2 Answers

Time.deltaTime appears to be running fast 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