Advanced Arrays
Time 3.6 hrs

Difficulty Intermediate
Prerequisites Data Structures & Graphs
Departments Science
Authors Sandra Kuipers
Groupings Individual
Minimum Year Group None


Advanced Arrays


This work is shared under the following license: Creative Commons BY-SA-NC


Learner Outcomes
Students will:
  • ...
Competency Focus
  • ...
Interdisciplinary Connections
  • ...
What was successful? What needs changing? Alternative Assessments and Lesson Ideas? What other Differentiation Ideas/Plans could be used?
  • ...
Any CC attribution, thanks, credit, etc.

This page requires you to be logged in to access it. Please login and try again.
5 mins
Getting Started
  • As code gets more complex, arrays are essential.
  • They allow us to manage a huge number of objects by using loops and indexes.
  • Let's start with a quick recap of Arrays in Unity:

From Unity Learn scripting tutorials.

5 mins
C# Built-in Arrays
  • C# has a lot of advanced ways of handling arrays.
  • The example above looks at built-in arrays.
  • Built-in arrays are the fastest and most efficient type of array.
    • Built-in arrays are created with a value type and square brackets, eg: int[].
    • Their size must be declared when the array is created, and cannot be changed.
    • This is what makes them fast: because they always use the same block of a computer's memory.
    • However, an array that cannot change size is more limited in its uses.
  • Here's an example of declaring and working with a built-in array.
public class ExampleClass : MonoBehaviour
    // Exposes an float array in the inspector, which you can edit there.
    public float[] values;
    void Start()
        foreach (float value in values)

        // Since we can't resize builtin arrays
        // we have to recreate the array to resize it
        values = new float[10];

        // assign the second element
        values[1] = 5.0F;
10 mins
C# Lists
  • Lists are another type of array in C#, and these ones come with a lot more features.
    • Lists are resizable as well as searchable, which makes them quite useful.
    • However, these features can come at a performance cost if the list gets really big.
  • Here's an example of declaring a list in C#. Note that the variable type is inside <>.
List<GameObject> = new List<GameObject>();
  • Lists are easy to use. There is a huge list of different operations that you can perform with it. Here are some of the common ones to start with:
    • Add: This adds an object at the end of List.
    • Remove: This removes the first occurrence of a specific object from List.
    • Clear: This removes all elements from List.
    • Contains: This determines whether an element is in List or not. It is very useful to check whether an element is stored in the list.
    • Insert: This inserts an element into List at the specified index.
    • ToArray: This copies the elements of List to a new array.

  • Dictionaries are similar to lists, but instead of an integer for an index, they can use a string, known as the array key.
5 mins
3D Arrays
  • Arrays can exist in more than one dimension.
  • Technically, they can be as many dimensions as your computer's memory allows, which is a lot!
  • For built-in arrays, we can declare a multi-dimensional array using the following syntax.
public int[,,] myArray = new int[10,10,30];
  • This syntax declares an array which has three dimensions, making it a 3D array.
  • We can access items in a 3D array with a similar syntax.
myArray[0,0,0] = 42;
  • This assigns the first item in each dimension of the array to the value of 42.
  • To iterate over a 3D array, we use a loop that is nested three times:
for (int x = 0; x < myArray.GetLength(0); x++) {
    for (int z = 0; z < myArray.GetLength(1); z++) {
        for (int y = 0; y < myArray.GetLength(2); y++) {
            // Do stuff!
  • In the above example, we can get the length of a single dimension in an array with the GetLength() method.
  • This example iterates over x and z first, because in Unity these are the horizontal dimensions. Then it iterates over y, which is the height of the 3D array.
5 mins
Minecraft Arrays
  • What better example of a 3D array is there than a Minecraft world!
  • Each "chunk" of the world in Minecraft is a 3D array of cubes.
  • Every chunk is also part of a bigger 3D array of chunks.

  • The world is created procedurally by determining which parts of the array should be solid blocks, and which should be open air.
  • The procedural scripts also use different types of noise and cellular automata to place caves, trees and other decorations in the world.
  • We'll look at procedural generation a bit more in-depth in a following unit, this unit is just the intro :)
5 mins
Minecraft Math
  • Minecraft uses a 3D array to create a map of cubes. But, it's not a solid block of cubes.
  • The map generator in Minecraft uses procedural generation to decide which parts of the array are cubes, and which are open air.
  • To understand how it works, we need to take a quick look at what pseudorandom numbers are.

  • Perlin noise is a type of pseudorandom number. This means it appears random, however it's deterministic: the number will always be the same for any given input.
  • Perlin noise looks like a 2D waveform. For any given x and y input, it will return a fraction between 0 and 1.

  • These numbers "wobble" and change smoothly between 0 and 1, giving the perlin noise pattern above.
  • So, how does the relate to Minecraft?
    • Well, many games that use procedural generation begin with the humble Perlin Noise function.
    • Behind the scenes of this function is a mathmatical algorithm that generates the value between 0 and 1.
    • Luckily, you don't need to know the math, just the input and output of the function.
    • In Unity, the function is called Mathf.PerlinNoise(x,y)
  • Look at the waveform above. Looks kind of like hills and valleys, right?
  • Well, if you were to use Perlin Noise to generate a 3D map, you could the value it returns to decide if a point in the map should be a cube or open air.
  • Consider the following conditional:
float noise = Mathf.PerlinNoise((float)x/10f, (float)z/10f) * (float)height;
if (y < noise)
    // Make a cube
  • In this example, the code is getting a perlin noise value and multiplying it by the height of the terrain to create.
  • Instead of a number between 0 and 1, this creates a number between 0 and the max height of the terrain.
  • Then, it's looking at the y value of the cube it's currently drawing.
  • Is this y value less than the noise?
    • If yes, this point is underground, so draw a cube.
    • If no, it doesn't draw a cube, which leaves open air above the ground.
  • Repeat this in a 3D nested for loop and you can begin to generate a whole 3D map!
180 mins
Your Advanced Array
  • 2D and 3D arrays are used all the time in game design to hold data.
  • They are used for grids, maps, level design, etc. For example:
    • Many games use a 2D grid for their inventory system that stores items.
    • Minecraft and block-based games use a 3D grid of cubes that can be created or destroyed.
    • Mario and sidescroller games use a 2D grid to design the game level.
  • In this unit, you'll use either a 2D or 3D array to create and manipulate a set of objects.
  • What those objects are and what the grid is used for is up to you!
  • The goal is to create your array procedurally:
    • This means the data doesn't exist before you press play on your game.
    • Instead, in the start() method, you would initialize and create the objects in your array.
    • You could use randomization or a noise function to create the objects.
    • Each object would be saved to your array at a numerical index (x,y for 2D, x,y,z for 3D)
    • Using the numerical index, you can manipulate your objects after they're created.
  • Experiment with your 2D or 3D array to create a scene in Unity that can be both generated and manipulated.
  • Once you're happy with your scene, zip it and upload the Google drive, then submit it as your evidence of learning in this unit.
There are no records to display.
Powered by Gibbon v27.0.00dev

Founded by Ross Parker at ICHK Secondary | Built by Ross Parker, Sandra Kuipers and the Gibbon community
Copyright © Gibbon Foundation 2010-2024 | Gibbon™ of Gibbon Education Ltd. (Hong Kong)
Created under the GNU GPL | Credits | Translators | Support