- Home /
Regenerate health over time
I have this integer static var energyObj : int = 5;
and then I have got another script, which subtracts one "energyObj" if you lose a level, then if you have zero "energyObj", you cant play before they have regenerated atleas one. Now my problem is, I cant seem to find a way to regenerate every 30 minute, i can only find ways to do it each second with the Time.deltatime var
but thats not how it is supposed to regenerate. Is there a way to make it regenerate every 30 minute instead of each second?
30 $$anonymous$$utes is just 1800 seconds. You can use Invoke(), InvokeRepeating(), coroutine, or a timestamp depending on your need. Post your code and someone can show you how to modify it for 1800 seconds.
Answer by 0tacun · Mar 22, 2014 at 11:46 PM
use a loop
var counter : float;
function Update(){
counter += Time.deltaTime;
if(counter > 30minutes){
energyObj++;
counter = 0.0; }
//if energyObj should't be above 5
if(energyObj > 5)
energyObj = 5;
}
Answer by SkaredCreations · Mar 22, 2014 at 11:54 PM
You can use a coroutine with an infinite loop inside which you have a yield WaitForSeconds(1800 / maxEnergy) and increase energyObj by 1. You can call this coroutine in your Start function and it will be ever active, it depends on your game logic.
For example (may be there's few minor syntax oversight, I use C# usually):
function Regen() {
var maxEnergy : int = 5;
var seconds : float = 1800 / maxEnergy; // 1800 is 30 minutes in seconds
while (true) {
if (energyObj < maxEnergy)
energyObj++;
yield WaitForSeconds(seconds);
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613140928im_/https://answers.unity.com/themes/thub/images/avi.jpg)