How to use Array?


I am trying to make a simple memory game, I got a couple of boxes you need to press however I want unity to choose the pattern (for example; It start with 1, you hit box 1 than it shows 1 and 3, you hit 1 and 3 etc etc).

However how I create such thing using Array? I am fairly new to Arrays so a helping hand would be great.


Firstly, you will have an array that looks like this in C#:

public GameObject[] blocks; // you should use the editor to add all of the blocks to this array

This will store the GameObject for each block you have to press. You can access each block by its unique index like this:

GameObject currentBlock = blocks[currentIndex];

Keep in mind indexes start from zero, so if you have 7 blocks, they will be indexed 0-6.

Use a List to represent your “memory” chain. This allows you to add one integer at a time to the List, then get them back in order using a “foreach” loop.
Using the List looks like this:

using System.Collections.Generic; // don't forget to put this at the top of your file

List<int> memoryList = new List<int>();

memoryList.Add(3); // adds the index 3 to the front of the list

// This will print out the elements of the list in the order they were added.
foreach (int i in memoryList) {
    print("The next number is " + i)

Hope this helps! By the way, does anyone know how to create separate blocks for code?

Found a great tutorial that will help me a lot! Thanks to @agosyclamoth for the name of the actually game I was trying to make. It appears to have a good tutorial :).

(It’s in C#)