- Home /
do for and while loops happen all in one frame?
Hello, i´ve always wondered if the for and while loops went through the whole loop in one frame or it looped once per frame. right now i have no uses for this information but i guess at some point it could be usefull.
Answer by ahmedbenlakhdhar · Dec 09, 2014 at 09:34 PM
All the code located in a function which is called every frame ( such as Update()
and OnGUI()
) will be called once a frame, including loops.
For instance, the following script displays the value of i
10 times, every frame:
void Update(){
for(int i=0; i<10; i++){
Debug.Log(i+"\n");
}
}
The result of running the game, using this script, during 2 frames will be:
0
1
2
3
4
5
6
7
8
9
0
1
2
3
4
5
6
7
8
9
Note: Coroutines are an exception for this rule.
Coroutines are the obvious exception to this rule. Coroutines are designed to allow code (including loops) to be executed across multiple frames.
Your answer
Follow this Question
Related Questions
for/while loops not working, or not updating 1 Answer
Can't figure out While Loop crash? 1 Answer
Is it possible to call a method once per frame inside of another method? 2 Answers
Looping inside OnGUI 2 Answers
How can I make the MoveTowards work? 0 Answers