Suleiman Abdullah
3 min readMay 27, 2024

Working with Array

Objective: Working with Array.

An array is the data type which store multiple variables of the same type.

Syntax

syntax, we use square bracket after data type to turn any data type to array, then we use new key word and data type with square bracket, then inside the second square bracket we put size of array.

public int[] number = new int[3];

But in Unity we can define without using new keyword. In Unity3d in the inspector ,we have option to specify a size of array then press enter will add a size in that array.

public int[] number;

Assign variable in array

Assign variable manually.

This way every time you want to change things you need to open code, but when application crush all your data will be here .

public int[] number = new int[] { 1,2,3};
//or this one
public int[] number = {1,2,3};

OR

//you can use this if you update to .Nate 8 other wise use the above one
public int[] number = [1,2,3];

Assigning variable in the inspector

public int[] number = new int[3];

OR

public int[] number;
code
Note: The number array when play the editor it will be size zero ,my is 3 because I set it early so its saved.

Accessing data in array

We provide the name of array square bracket and specify which index we want. Every variable it is stored in index form ,this mean a first variable will be stored in index 0 since in programming number start from zero.

Looping array using for each loop

For each is a loop just like other loop ,it take array as maxvalue.

you can rename item into any name , a collection must be an array or list

See you in thenext 1.

Suleiman Abdullah

Self taught Unity Developer, who is passion about making games ,and he loves Math For Game Development