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 CB-TV · Jun 20, 2015 at 08:40 PM · variablerpgtime.deltatimeincreasedecrease

Adding a value to a variable every second

I have spent a while trying to figure this out and promised my last resort would be Unity Answers. But here I am.

I need to increase a variable by 1 every second. I've tried to do:

 thirst += Time.deltaTime * 1;

but it just doesn't seem to work.

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 imp903 · Jun 20, 2015 at 08:46 PM 0
Share

I'm not totally clear on some of this Time.time vs Time.deltaTime, but ins$$anonymous$$d of += I would think if you just changed it to = it would solve it. Because right now it seems to be exponential so the first second it adds one, then the second second when Time.deltaTime = two, it adds two, and so on so it's adding more every second. If it's just equal to Time.deltaTime *1 then after ten seconds thirst is ten, after twenty, thirst is twenty, and so on. At least that's what I would try next

4 Replies

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

Answer by AurimasBlazulionis · Jun 20, 2015 at 09:04 PM

There are quite a few ways. The first one, using while loop

 var value : Boolean = true;
 
 function AddSomeValue(){ //call this when you need to start adding
      while(value){ //this lets you stop the loop at any time
           //just change the value of this variable to false
           yield WaitForSeconds(1);
           thirst++;
      }
 }

Another, using invokerepeating

 InvokeRepeating("AddValue", 1, 1); // function string, start after float, repeat rate float
 
 function AddValue () {
     thirst++;
 }

you can cancel invokerepeating using CancelInvoke(), but if you have many of them in one script, it will stop all of them

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 CB-TV · Jun 20, 2015 at 09:07 PM 0
Share

Thanks for the answer!

avatar image FolkvangStudios · Aug 27, 2017 at 05:11 PM 0
Share

This was a fantastic solution. It allowed me to "gradually" adjust the difficulty level over time.

avatar image
1

Answer by Arkamis · Jun 21, 2015 at 11:17 AM

Time.deltaTime gives you the time since the last frame. Computing thirst += Time.deltaTime; will increase the thirst counter by the length of a frame, every frame -- hence, one unit per second.

If you are not seeing this behavior, chances are you are resetting thirst somewhere, or you have done something to the Time scale. In other words, your code should work, unless there are bits of it you're not telling us about.

Try adding a log statement whenever you increment thirst, and see how the values evolve.

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 AurimasBlazulionis · Jun 22, 2015 at 04:38 PM 0
Share

He uses int so that is why it didn't work

avatar image
1

Answer by zelalem_t · Feb 07, 2018 at 07:14 AM

int gameStartTime=(int)Time.time

void update(){ if((int)Time.time-gameStartTime==1){ //do something here gameStartTime=(int)Time.time } }

Comment
Add comment · 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
0

Answer by jobenjo · Jun 21, 2015 at 11:17 AM

Time.deltaTime is the amount of time it takes to load the last frame, so for example 0.2 seconds. Therefore, what you said above would be seen to the computer like this: thirst += 0.2 * 1; multiplying that number by one, doesnt change it in the first place anyways; so its normal it doesnt work.

Comment
Add comment · Show 4 · 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 Arkamis · Jun 22, 2015 at 04:27 PM 0
Share

The += operator operates by increment. thirst += 0.2 * 1 is the same as thirst = thirst + 0.2. This will definitely increment thirst by 0.2 every frame.

avatar image AurimasBlazulionis · Jun 22, 2015 at 04:37 PM 0
Share

All I can tell you, guysh, is that he uses int, so that is why it didn't work lol

avatar image Arkamis · Jun 22, 2015 at 04:38 PM 0
Share

Well if he uses int, it sure won't work, but the OP hasn't posted the code where it's declared.

avatar image Arkamis · Jun 22, 2015 at 04:41 PM 0
Share

@TheDiamondPlay The word int doesn't appear in this post until your comment. It is not clear that the OP is using an int vs. a float. This is one possible cause. Another possible cause is that the OP is re-initializing thirst at the beginning of every frame.

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

28 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

1 Variable Affected By Multiple Variables 2 Answers

Variable suddenly increases with time.deltatime 0 Answers

controlling the increment in the variable 1 Answer

Adding an integer 3 Answers

I am trying to make a stamina bar and it won't drain no matter what i try. 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