Hi, is there a way of using waitforseconds in a correct way? I am trying to use the following but it doesn’t work. I know how to use alternative methods but i am curious how waitforseconds could be used in such case.
function Update ()
{
LetsWait ();
}
function LetsWait ()
{
var i : int;
++i;
yield WaitForSeconds(2.0);
}
The script above doesn’t increment i variable regularly, instead it waits 2 sec and increment i variable continuously. What i want to do is incrementing i once per 2 sec. How to use waitforseconds in this purpose?
Thank you very much
I would recommend scrapping the WaitForSeconds and using InvokeRepeating.
so create a function for the incrementing the variable
InvokeRepeating("Increment", 0, 2);
function Increment () {
var i : int;
++i;
}
This will call the Increment function once every 2 seconds. If you want to wait a certain amount of seconds, change the 0 to desired number of seconds.
Be aware that Update calls your function EVERY FRAME. After 2 seconds have passed the value will start to increment each frame. What you want to do is to call the function in Start, and increment i in a while or for.