Loops and Arrays – 12/03/21

Loops and Arrays are pieces of code that can help each other out in various ways because of this, this can make code cleaner, less resource intensive, more responsive and in some cases more accurate.

An Array is a collection of variables used for the same purpose. As an example if we were making a 4 player game and were to give each player 3 lives, rather than having numerous ints per player for lives, we would use an array. It is also worth noting that Arrays keep variables in sequential memory rather than random memory, making it quicker and easier for the computer to find. By making the Array public in the script, the Unity editor allows for us to edit the size of the array and the information it holds.

I have used an Array in a Unity name guessing game, this is what that looks like:

A Loop comes in multiple forms but the most common loops are For and While, a For loop is basically saying ‘For each one of these, do this’. A While loop is saying ‘Whilst this is happening, do this’. The While loop could be useful for a racing game maybe, while this button is held down, then acceleration is increased or while health is below 20%, pulse a red aura around the screen.

Leave a comment