Week7 (Part3): Data Structure

Nissie Bungbrakearti
code3100
Published in
4 min readMay 7, 2017

Continuing from Week7 (Part2) I will continue to look at the basics of C# coding for Unity3D.

What is an array?

An array is when you want to store more than one piece of information in the one variable. To do this a collection needs to be used. By adding open brackets [ ], Unity knows that we want to create an array. As seen below I have created an array of names, however have not put any values inside it. Selecting the camera we can see that there is a names option in the Inspector panel where I can determine the size, or in better words, the number of elements. These element values are set to be a string and so in typing in numbers we would receive an error.

Making this array public means that we can determine how many values and what those values are within the Unity editor. Below I have identified within the script itself how many arrays there will be by using [5]. When we play the game, however, we can see that Unity over rides the array value back to zero due to it being a public variable. Therefore we should note that public fields in the class will be overwritten by the Unity Inspector.

Removing public means that we can work with the variable exclusively from within the script. In void start I have specified the beginning index of the array being [0]. Therefore, the arrays will range from index [0] — [4]. By setting name[0] to Nissie I can get Unity to print “first name” + the first index of the array which I have set to “Nissie”. We can see this below in the Unity3D console.

Alternatively, you can set the array values at the beginning and remove the amount of items within the variable. Removing the number of items within the variable will mean that the compiler will automatically know the number of items within our array string. I can then call print = (names[0] +” “+ names[1]); where “ “ is creating a space in between the two items.

Now, by creating another print string we can print “Total names” and get the length of the names, meaning the number of items within the variable. Below we can see that there are now two outputs.

What is a list?

A list is another collection of items in a class. A list, however, is able to grow dynamically. As we can see below, a list acts very similar to that of an array. The exception in the example below is that List uses count to extract the number of items instead of length which is what array uses.

Now, below we can see how a list differs from an array i being able to dynamically update. We can add an item to the animal variable within the void start method and still have it work. We then want to try and print what the last animal is. By using print (….animals[animals.Count-1]); we can achieve this. The -1 is very important due to the index system used. For example, with the addition of mouse, there are now 3 items within the variable, however we know that indexes start at 0. If we were not to include -1 then it would try to print animals[3] instead of animals[2] which is what is correct.

Below, I have changed nothing except for added another animal. By adding the one line of code, we can see how flexible lists are in that they allow for dynamic updating.

We can also easily remove items from a list.

--

--