- Arrays are a way of storing a
collection of data of the same type together.
Let's say we need to store five integers.
Rather than storing them individually
as my int. A, my int. B, my int. C, etcetera,
we can store them in an array.
We declare an array in a similar
way to declaring an individual variable.
However, after the type of the variable,
we use open and closed square brackets.
It's important to make the distinction
that an array is not a type, but
a collection of variables of a certain type.
Before an array can be used, we need
to know it's length, I.e. how many
elements will be stored in it.
Elements are what we call items
in an array.
To specify the length, we use the "new" keyword
followed by the type of the array and the
number of elements in square brackets.
We can now initialize our array in the Start function.
We can access and initialize elements of the array
by using the name of the array followed
by square brackets containing the
index of the element.
The index of an element is simply
an integer, with the first index being zero.
When thinking of an element's index,
think about how many steps away from the first
element, the one you're trying to access is.
So to access the first element, use the index
zero, since it's zero steps away from the first.
For the second element, use the index one, and so on.
Now I'm going to initialize the five
elements in my array.
So that's one way of declaring
and initializing my array.
However, if I want to, I can do it all in one line.
This means that I can initialize
my array at the same time as declaring it.
We do this by using braces as we
normally would for a function, loop, or if statement.
Between the braces, we put the
value for our elements in the order
we'd like them to appear in the array.
This will create an array of five ints just as before,
starting with 12, then 76, and so on.
Note that we didn't specifically state
the length this time.
This is defined by the number of
elements within our curly braces.
Arrays declared in this way can still
be accessed in the same way as
demonstrated in our first example.
There are a few things to note about arrays.
If you make an array public, you
will be able to see it in the Inspector
and allocate values to it.
So if I save my script and switch back,
I can see that where it's assigned to
an object, my player's array is
showing publicly and in the Inspector,
and I can assign a length by using the size value.
So if I type in a value of one into size,
I get one element in my array on to
which I can drop a GameObject
because that's the type of this array.
However, we can use the script to initialize it.
Unity has some functions that can help us to do this.
We want our player's array to store
all of the players in our scene.
The function "find GameObjects" with tag
returns an array of all the game
objects in the scene with a specified tag.
Since our player GameObjects all
have the player tag, we can pass this in
and get all of the players in the scene.
We can use the array that this function
returns to initialize our new public players' array.
So simply by doing this, we have
assigned everything that this function
has returned to our player's array.
That means that it's collected the objects
Aaron, Bob and Charlie, as they
all contain the tag "player".
Another great feature of arrays is that
they work really well with loops.
Let's say that we want to log the name
of all of the players in our scene.
We will use a full loop to iterate
through each element of the array.
Arrays have a property for their length.
This will return the number of elements in an array.
This means that if we start our loops'
iterator at zero and continue to loop
whilst it's less than the length of the array,
we can perform a number of actions
equal to the number of elements in our array.
What's more, our iterator varliable, "I",
will also be equal to the index number in our array.
This will print out a sentence that says
"player number zero is named Aaron".
It does this by taking the text player number
followed by the "I" variable, representing
the number or index, and finally it
retrieves the player's name by looking
at a particular index in the player's
GameObject array.
So I'll save my script and return,
I'll press Play and here we can see
that the full loop has logged three messages,
one for each item in the array until
it's reached the end of the array of elements.
You can also see that the array is
assigned to our public variable in the Inspector,
as it was initialized by the script.