Retrieving the data from the Array or List<T>
Declaring and storing data in the array or list is very clear to us now. The next thing to learn is how to get stored elements from an array. To get a stored element from the array, write an array variable name followed by square brackets. You must write an int
value within the brackets. That value is called an index. The index is simply a position in the array. So, to get the first element stored in the array, we will write the following code:
myArray[0];
Unity will return the data stored in the first place in myArray
. It works exactly the same way as the return
type methods that we discussed in the previous chapter. So, if myArray
stores a string value on index 0
, that string will be returned to the place where you are calling it. Complex? It's not. Let's show you by example.
Note
The index value starts at 0, not 1, so the first element in an array containing 10 elements will be accessible through an index value of 0
and last one through...