- Home /
The question is answered, right answer was accepted
how does invokerepeating work
can anyone help me, by explaining how invokeRepearting works. i couldn't able to understand the last variable repeatRate. need help.
http://unitygems.com/mistakes1/ look at the $$anonymous$$ake Somethin Happen in the Future section.
Why use this ins$$anonymous$$d of just putting what you need in Update()?
Answer by AlkisFortuneFish · Feb 25, 2014 at 11:12 AM
Read the documentation.
"After the first invocation repeats calling that function every repeatRate seconds."
So, if you do InvokeRepeating("Function", 1.0f, 1.0f), it will call Function one second after the InvokeRepeating call and then every one second thereafter.
I just watched an online Unity lesson, where they showed something like: float delayTime = 1f; InvokeRepeating("Foo", delayTime, delayTime);
void Foo(){ delayTime += 1f; // other stuff }
From my not-so-vast program$$anonymous$$g knowledge - this shouldn't work in the way they intended, and the function will keep repeating itself every 1 sec, and the delay shouldn't be increasing every time it's called. However, I may be wrong, since the ones that did this are more experienced. So.. would this function be called on the 1-st, 3-rd, 6-th, 10-th... second, or will it be called every second?
Answer by Ertai7 · Feb 25, 2014 at 11:18 AM
The difference to the regular Invoke is that InvokeRepeating repeats itself. The first argument is the initial delay, the second the time that will pass until the Invoke is called again.
For instance, to implment a countdown timer you could use:
public int timerSeconds = 60;
InvokeRepeating ("Countdown", 1, 1);
void CountDown()
{
timerSeconds--;
if(timerSeconds < 1)
{
print ("Count down finished");
CancelInvoke("CountDown");
}
}
If you want to give an example, give a proper one well indented. The Invoke method cannot just be there on its own.
thanks for the explanations, so
1st variable is $$anonymous$$ethod Name. 2nd variable is timedelay. 3rd variable is then how many seconds after the call has to be made.
First is the method as string, second is the delay before first call and giving a value of 0 has been buggy (dunno if they fixed it) so if you need to start right on use a tiny value like 0.001. Last parameter is frequency of call after the first.
InvokeRepeating ("Countdown", 10f, 5f);
First call after 10s, all other calls after will come every 5s.