- Home /
How to make a variable decrease every five seconds?
So, right now my hunger code is;
timer=Time.deltaTime;
if(timer>10)
{
Hunger-=1;
HungerReset();
}
But I want Hunger to decrease every five seconds. Any ideas? If I left out any important information, let me know. Okay, so none of the suggestions worked, but thanks for trying. My new code is:
#pragma strict
var Hunger:int;
var MaxHunger:float=1000;
var timer:float;
function Start ()
{
Hunger=MaxHunger;
}
function Update ()
{
timer=Time.deltaTime;
if(timer>10){
function Hunger();{
for(i=Hunger;i>0;i--) {
yield WaitForSeconds(5.0);
}
}
}
}
The only problem is that Unity says: Assets/Hunger.js(16,26): BCE0044: expecting (, found 'Hunger'. Does anyone know why? Thanks!
Answer by thomasindustry · Jun 16, 2014 at 09:19 PM
Coroutines work well for consistent operations.
void StartHunger()
{
StartCoroutine("HungerCounter");
}
IEnumerator HungerCounter()
{
while (true)
{
yield return new WaitForSeconds(5f);
Hunger-=1;
}
}
Answer by hurleybird · Jun 16, 2014 at 09:19 PM
float timer = 0f;
if (Time.time > timer)
{
Hunger -=1;
timer = Time.time + 5f)
}
Answer by robertbu · Jun 16, 2014 at 09:01 PM
You haven't given a lot of context (and I have no idea with HungerReset() does). But making some assumptions, you would modify your code this way:
timer += Time.deltaTime;
if(timer >= 5.0)
{
timer = 0.0;
Hunger-=1;
HungerReset();
}
Your answer
Follow this Question
Related Questions
Resetting Timer Using PlayerPrefs? (JS) 1 Answer
how to make a varied countdown timer 1 Answer
Why is my timer reseting itself? 0 Answers
variable reset not working. 0 Answers
Game Time with variable count speed 1 Answer