I know you cannot use function Update with a coroutine, but does anyone know of a workaround that can accomplish the same thing? Something like this (but without the not working part)? Thanks
var test = 0;
while(test==1){
foo
yield
more foo
yield WaitForSeconds (1);
test=0:
}
function Update(){
if ((Input.GetKeyDown ("space")){
test=1;
}
}
Update can’t be a coroutine, but it can call coroutines - just be sure to not call a coroutine when its previous instance is still running (unless it’s an intended behaviour):
var running = false;
function MyCoroutine(){
running = true; // signals that the coroutine is running
print("Coroutine started");
var t: float = 0;
while (t < 10){
t += Time.deltaTime;
yield;
}
print("10 seconds ellapsed");
yield WaitForSeconds (1);
print("Coroutine ended");
running = false; // coroutine finished
}
function Update(){
if ((Input.GetKeyDown ("space")){
if (!running){ // only call MyCoroutine when the previous one has finished
MyCoroutine();
}
}
}