Loops
Learn how to use loops in C# to perform iterative tasks, iterate over collections, or process events.
We'll cover the following...
Introduction
In C# and Unity, loops are used to execute a block of code multiple times. There are several types of loops that we can use in Unity. Let’s discuss them below.
The for
loop
Developers use the for
loop to repeat a set of instructions a specific number of times. Here’s the syntax for a for
loop:
Press + to interact
for (int i = 0; i < 10; i++){Debug.Log(i);}
In this example, the for
loop will repeat the Debug.Log(i)
statement ten times. The loop starts with the variable i
set to 0
and will continue as long as i
is less than 10
. After each iteration, i
is incremented by 1
. ...