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 Digital-Phantom · Jul 11, 2013 at 06:18 PM · timercountdown

Timer script running too fast

Ive been using a simple on/off script with my in game flashlight but now Im trying to implement a battery effect and have the flashlight on a countdown timer while switched on. I've updated my original script but the timer is counting down way too fast and I cant figure why?

 var Battery : int = 100;
 
 var Batterytext : GUIText;
 
 
 function Update ()
 {
 
 Batterytext.text = " Flashlight " + Battery + " % ";
 
     if (Input.GetKeyDown("f"))
     {
         if (light.enabled == true)
         light.enabled = false;
         
         else
         
         light.enabled = true;
     }
     
     if (light.enabled == true)
     {
         Battery -= Time.deltaTime;
     }
     
 }

I did try another method using yield WaitForSeconds but had similar issues.

The on/off function still works and pauses the countdown (if your quick enough...lol) But it doesn't count down in seconds as Id planned it to.

Any suggestions guys ???

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
1
Best Answer

Answer by InfiniBuzz · Jul 11, 2013 at 06:20 PM

HI

try this:

 var Battery : int = 100;
 var Batterytext : GUIText;
 var batterySpeed : float = 0.01f;
      
      
 function Update ()
 {
      
     Batterytext.text = " Flashlight " + Battery + " % ";
      
     if (Input.GetKeyDown("f"))
     {
        if (light.enabled == true)
           light.enabled = false;
        else
           light.enabled = true;
     }
      
     if (light.enabled == true)
     {
        Battery -= Time.deltaTime * batterySpeed ;
     }
      
 }

like this, you can easily edit the time, your battery goes down.

Comment
Add comment · Show 6 · 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 Digital-Phantom · Jul 11, 2013 at 06:25 PM 0
Share

Nope, still running down a super speed (didn't even seem to slow it)

:(

avatar image InfiniBuzz · Jul 11, 2013 at 06:30 PM 0
Share

have you tried to set batterySpeed to maybe 0.000001?

and try to make Battery variable a float, not int

avatar image Digital-Phantom · Jul 11, 2013 at 06:52 PM 0
Share

Ok..

changing the speed alone didn't do anything, no matter how many decimal places.

Changing from battery from 'int' to a 'float' DID work. I just had to change batterySpeed to 1

The only issue now is that the GUI shows the battery % with 5 decimal places after it (Flashlight 97.54321%) which looks really weird

avatar image InfiniBuzz · Jul 11, 2013 at 06:57 PM 0
Share

glad it helped.. you can use the String class functions to format the output.

 Batterytext.text = "Flashlight: "+ string.Format("{0:0%}",Battery);
avatar image InfiniBuzz · Jul 11, 2013 at 07:04 PM 0
Share

sorry, s in string needs to be uppercase I think

 Batterytext.text = "Flashlight: "+ String.Format("{0:0%}",Battery);
Show more comments

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

16 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

Related Questions

Is it possible to turn a script On/Off ? (Solved) 3 Answers

Enable/Disable script with timer 1 Answer

Countdown timer into text c# 1 Answer

How to restart a level with countdown? 4 Answers

countdown timer on for example a wall of a building in your game? 2 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