- Home /
How to loop a function?
I've heard of the for and while things but I just don't understand them. What I want to do is something like this:
var cookies = 0;
MyFunction();
function MyFunction(){
cookies ++;
yield WaitForSeconds(5);
MyFunction();
}
Whenever I try and do this, I keep getting an error message. I know about using InvokeRepeating, but I already used it in this script with the CancelInvoke, so that would mess everything up if I used it again. So how would I be able to do this?
you should use
StartCoroutine($$anonymous$$yFunction());
ins$$anonymous$$d of
$$anonymous$$yFunction();
also as I can understand while and for loops are not what you are looking for, they have a different purpose, to process multiple things on a single frame. you should check these; https://unity3d.com/learn/tutorials/modules/beginner/scripting/loops
The only problem about using a coroutine is that the loop will eventually end. I want the loop to never stop.
Answer by chillersanim · Apr 22, 2014 at 08:03 PM
Hello
The code construct you have has an endless recursive call and will cause a stack overflow.
To call the method over and over again, you could use the folowing code (Not tested!):
var cookies = 0;
function OnEnable()
{
// Calls the MyFunction and calls it all 5 seconds
InvokeRepeating("MyFunction", 0, 5);
}
function OnDisable()
{
// Stops the repeating call
CancelInvoke("MyFunction");
}
function MyFunction()
{
cookies++;
}
I hope that the code isn't incorrect because normaly I write in C#.
Greetings
Chillersanim
Thanks! I didn't know that it wouldn't interfere if I used invoke twice.
Answer by Ed unity · Apr 22, 2014 at 08:06 PM
In that script you are never going to escape the MyFunction function. Thus Unity will hang and will appear that it is frozen. What you most likely want to do is something like:
void Update(){
int numberOfTimesToRun = 1;
for(int i = 0; i < numberOfTimesToRun; ++i)
MyFunction();
}
function MyFunction(){
yield WaitForSeconds(5);
}
Source for the information below: http://www.w3schools.com/js/js_loop_for.asp
"The for loop is often the tool you will use when you want to create a loop.
The for loop has the following syntax:
for (statement 1; statement 2; statement 3)
{
the code block to be executed
}
Statement 1 is executed before the loop (the code block) starts.
Statement 2 defines the condition for running the loop (the code block).
Statement 3 is executed each time after the loop (the code block) has been executed."
Hello
You should not use a yield in a for loop like that when it is called in a repeatedly called method like the Update, because all code after the yield is going to be called late to. Also the update method is going to be called around 60 times per second, so you can cause a stack overflow and you are generating a lot of different handles.
Ins$$anonymous$$d you should use a InvokeRepeating() in the OnEnable() or somewhere where it is not called the whole time.
Greetings
Chillersanim
I know about invoke repeating and that's what i would have used. The only problem is that I already used it in my script along with Cancel Invoke. If I use it again, then the Cancel Invoke might interfere and then the script won't work properly. I also tried the other method with the for loop and when it was about to do the loop, Unity froze.
No, you just need to overload the method name and then it will only cancel the invokeRepeating for that method.
As long as you don't call InvokeRepeating() for the same method multiple times (what you realy not should do!), there is no problem.
Edit:
When you use yield in a for loop, then it often happens that unity freezes, as mentioned in the comment above yours.
Greetings
Chillersanim