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 /
This question was closed Jun 03, 2013 at 12:08 PM by Fattie for the following reason:

Duplicate Question

avatar image
0
Question by jeffreywarf · Jun 03, 2013 at 02:57 AM · updatetimedeltatimesecondsregeneration

Add 1 Per Second to an Int

For some reason I can't get this to work right even though the logic seems sound.

I have a GUI box that scales down proportionally based on how much of one int there is versus another. It looks like this:

 var current : int;
 var maximum : int;
 
 function Start (){
 maximum = 100;
 current = maximum;
 }
 
 function OnGUI (){
 
 GUI.Box(new Rect(10,10 + Screen.height / 24,(Screen.width * current / maximum / 2), Screen.height / 24));
 
  if(GUI.Button(new Rect(Screen.width / 8, Screen.height / 4, Screen.width / 16, Screen.width / 16), "Subtract"))
  {
   current -= 10;
  }
 
 }
 
 function Update () {
  if(current < maximum)
  {
   current += Time.deltaTime;
  }
 }

My problem is that it either does not work at all and adds nothing after the current amount is subtracted, or it adds too fast and goes back completely in less than a second or two. I only want the amount to change once per second. (Note that this is code I wrote just this moment as a quick demonstration of what is happening, the full code is too long to post, but the mechanics aren't changed).

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 Fattie · Jun 03, 2013 at 12:08 PM 0
Share

use Invoke() and InvokeRepeating() for simple timers in Unity. it's that easy. unityGE$$anonymous$$S.com

1 Reply

  • Sort: 
avatar image
1
Best Answer

Answer by PAEvenson · Jun 03, 2013 at 03:03 AM

Time.deltaTime is a float and current is an int. Ints will always round down. Can you not use a float for some reason?

Comment
Add comment · Show 5 · 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 PAEvenson · Jun 03, 2013 at 03:06 AM 0
Share

or you can multiply Time.deltaTime * 100. You will lose the remainder....but it will move the decimal place over so 100 == 1 second.

avatar image jeffreywarf · Jun 03, 2013 at 03:16 AM 0
Share

$$anonymous$$ultiplying it by 100 won't work because while Time.deltaTime is a float, it is still >1, so multiplying it by 100 will not work.

I'd love to not use a float but the point is for current to go up by 1 per SECOND, it's a ti$$anonymous$$g thing, and unfortunately the only way to get time accurately is with Time.deltaTime.

avatar image syclamoth · Jun 03, 2013 at 03:23 AM 0
Share

Why don't you just use 'Time.time' and round it?

If you want the number of full seconds between two points in time, first cache 'Time.time' in a float variable at the beginning of the period, then every frame the number of full seconds will be

 $$anonymous$$athf.FloorToInt(Time.time - startTime);
avatar image PAEvenson · Jun 03, 2013 at 03:28 AM 0
Share

Ahh i see the issue. You may want something like this(not tested):

 var timer:float = 0.0f;
 
 function Update () {
 
  if(current < maximum)
  {
   timer += Time.deltaTime;
   if(timer > 1.0f)
   {
     current += 1;
     timer -= 1.0f;
   }
  }

avatar image jeffreywarf · Jun 03, 2013 at 04:39 AM 0
Share

That works, took me a little bit to understand why but it does. Thanks!

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

Health Regeneration Issues. 2 Answers

Execute code every x seconds with Update() 4 Answers

Single Step (pause, resume), or externally clock game loop 0 Answers

Time.deltaTime not consistent over time 1 Answer

How do I make a counter that goes up by 1 every second? 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