- Home /
Since I can't add a yield during an update, how would I go around it?
Lets say this is my script:
var On : boolean = false;
var Off : boolean = true;'
var Health : float = 20;
function Update()
{
if(On == true)
{
HealthOn()
}
}
function HealthOn()
{
Health -= 2
//yield for 3 seconds
}
Answer by whydoidoit · Sep 04, 2012 at 02:46 PM
You can start a coroutine from Update but in your case it looks like you should either do that in Start or start a coroutine there - here's the Start version:
function Start() {
while(true) {
if(On) {
Health -= 2;
yield new WaitForSeconds(3);
}
yield null;
}
}
Even though it is on start, it will continuously check to see if the boolean is true?
Yes due to the while loop and the yield - you can also start a coroutine but there is no need in this simple case.
Think of coroutines with while(true) loops like different versions of Update that have the ability to run at different frequencies. Start can be a coroutine which I used in this case - but any coroutine you run also has the same properties.