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!
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);
}
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.
Worked very smoothly ! Thanks a lot and sorry for my newbie errors, I’m still learning 