Unity Loops

Loops in Unity Scripting

Many times we have to repeat something in Unity and you know well that repetition is boring and time-consuming, that’s why throughout human history they found a way for the things they had to repeat all the time. For example, they would try to find an easier way to do it, or they would build a device, or they wouldn’t do it at all, or they would complain if they was forced to do it. Suppose you tell a person that there are five pots of food on the stove and go check the first pot and see if the salt is good, then come and tell me, then go see the second pot and come again and tell the result and repeat this for all the pots. If each time checking the pot consists of three actions: going, checking the salt and returning to tell the result, it is repeating 15 actions for all the pots. Well, the simpler and better way is to go and return once each and check all the pots together so that we have three operations in total. This is where this imaginary idea of ours about visiting pots turns into something in programming called “Loop”. Now let’s see how it works. First, see how a loop looks like:

for (initialization; condition; update) {
    // Code block to be executed.
}

Of course, this is a loop that is called a “for loop”, which is the most important type of loop. In this example, “for” is a keyword. Its job is to tell both us and our PCs that a loop is starting here. In order for the loop to start working, some things must be determined before that, which we put in the parentheses after the “for”.

The first is “Initialization”, which is a counter. Its job is to determine the number of times the pots should be checked. Usually, its initial value is zero because it hasn’t started yet, but after checking each pot, one is added to it.

Next is the condition. This condition must be met for the loop to work both the first time and the subsequent times. One side of this condition is the total number of pots and the other side is the current value of the counter. Because at first, the value of the counter is zero, it means that none of the pots have been checked yet, and the number of pots is five, so the condition is met and the loop starts working and executes the codes inside it. When it is finished and there is nothing left to execute, it returns to the top and adds one to the counter in “update”, which is the third part inside the parentheses.

I don’t want to spoil the story above, but keep in mind that the initial value of the counter is not always zero, and other numbers can be placed there, but you usually see that it starts with zero. Another point is that update is not supposed to always add to the counter, sometimes it subtracts from its value, which I will explain later with an example.

We also put the three lines of code related to going, checking salt, and returning inside the curly brackets, and in our story, these three lines are executed five times. After the fifth time, because the number of pots is equal to the number of times that are checked, our condition is no longer fulfilled, so the loop is no longer executed.

In the example below, you can see a workable example of “for loop”. One point is that it defines the counter inside the parentheses and says that the counter inside this loop named “i” is an integer type. Although this can be done outside the loop, it is easier and you have one less line.

for (int i = 0; i < 5; i++)
    {
        // Code block to be executed.
    }

Another thing is about the update section, where the name of the counter can be seen with two ++ signs. This way of writing to add to the value of the counter has become very common because it is short and simple, but the same thing can be done in other ways that I have written below.

i++
i+=1
i=i+1

A very important thing about the condition section in the loop. In the above example, because we knew that we have a total of five pots to check, that’s why our condition was this: until our counter is less than five. But in many cases, we don’t have the number of pots because their number is stored in an array (I explained the array here). What should we do now? Well, we have to make the array tell how many pots it has stored in itself, for this we enter the name of the array first, a dot, and then the number of elements of the array, as in the example below.

for (int i = 0; i < myTestArray.Length; i++)
    {
        // Code block to be executed.
    }

I will soon update this post with an explanation about the other types of loops.

If you have any questions or comments, feel free to leave them in the comments section below.

Leave a Reply

Your email address will not be published. Required fields are marked *