• 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 ArtC0de · Jul 21, 2017 at 04:52 PM · looptime.deltatimewhile-loopdecrease

Decrease a value every second?

Hello, I'm trying to make a little function in C# ... I'd like to decrease a value by 1 every second, here my Code:

 private int hunger= 100;
 
 void decreaseHunger(){
      while (0 < hunger) {
             Time.deltaTime -= 1; // IM SURE THIS ONE IS ULTRA WRONG
             hunger -= 1;
         }
 }
 void Update(){
    Debug.log(hunger);
 }

Thanks 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

3 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by tanoshimi · Jul 21, 2017 at 05:01 PM

You're right - it is ultra wrong! Try this:

 private int hunger= 100;

 void Start(){
   InvokeRepeating("decreaseHunger", 1.0f, 1.0f);
  }
 
 void decreaseHunger(){
   if(hunger > 0) {
          hunger -= 1;
      }
  }

 void Update(){
   Debug.log(hunger);
 }
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
1

Answer by megabrobro · Jul 21, 2017 at 05:31 PM

the Time.delta returns a value of how long it took to do the last update cycle. AFAIK the normal method is to have a variable called timeCountInSeconds or something, then in the update loop just do timeCountInSeconds += Time.delta;

That will give you a count showing the actual seconds

Hope this helps.

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 ArtC0de · Jul 21, 2017 at 05:21 PM

Worked very smoothly ! Thanks a lot and sorry for my newbie errors, I'm still learning :)

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 megabrobro · Jul 21, 2017 at 05:32 PM 0
Share

im new too. but i found it easier to understand my version which ive typed below. Please forgive me if i misunderstood what u meant

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

70 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 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 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

While loop issues in Update() vs. Start() 0 Answers

Question about the "WhileLoop" tutorial 1 Answer

Health bar depletes when app isn't open 1 Answer

infinite loop 2 Answers

Is it possible to call a method once per frame inside of another method? 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges