All these objects, variables and types are useful but what happens when we have hundreds or thousands of them? How can we possibly keep track? C# arrays are the solution and in this 5-minute tutorial, we will see the simple way that we can organize objects and variables into arrays. Then we will waste no time in putting them to use in some real projects.
[widgets_on_pages id=”udemy_advert_unity_1″][widgets_on_pages id=”udemy_code_details”]

About this tutorial

Skill level 1
Time to read: 10 minutes

New Concepts:

  1. C# arrays
  2. Declaring C# arrays
  3. Array notation replaces variable names
  4. What can we do with arrays in the real world

Projects related to or that demonstrate these concepts

To get started look at this potential nightmare scenario where we have a high score table with a hundred scores in it.

int highScore1;
int highScore2;
// 97 more lines like the above
int highScore100;

Straight away this can seem unwieldy but what about when someone gets a new high score and we have to shift the scores in every variable down one place. The nightmare begins.

highScore100 = highScore99;
highScore99 = highScore98;
// 97 more lines...
highScore1 = score;

Now imagine we want to keep track of String variables that represent the name of each person who achieved each high score. Clearly, we have a problem. There must be a better way. When we have a whole array of variables what we need is a C# array. An array is a variable that holds up to a predetermined maximum number of other variables. Each element is a variable of a consistent type.
The following code declares an array that can hold int type variables; perhaps even a high-score table.

int [] highScores;

We could also declare arrays of other types like this.

int [] numberArray;
boolean [] liveAliens;
float [] bulletVelocities;

Each of these arrays would need to have a fixed maximum amount of storage space allocated before it was used. We do so like this.

numberArray = new int [100];

The above allocates up to a maximum of 100 int sized storage spaces. Arrays can seem slightly strange to new programmers because the storage spaces start off at zero, not 1. Therefore in a 100 wide array, the storage spaces would run from 0 to 99. We could actually initialize some of these storage spaces like this.

numberArray[0] = 5;
numberArray[1] = 6;
numberArray[2] = 7;

But note we can only ever put the declared type into an array and the type that an array holds can never change.

numberArray[3]= "Manic Miner";// Won't work

So when we have an array of int types, what are each of the int variables called- what are their names? The array notation syntax replaces the name. And we can do anything with a variable in an array that we could do with a regular variable with a name.

// Assign a value to the int stored at position 3
numberArray [3] = 123;

// Subtract the value stored in position
// 6 from the value stored in position 8
// and assign it to position 3
numberArray[3] = intArray[8] - intArray[6];

// Declare a regular int variable called myInt
// and assign it the value held in position 3
int myInt = numberArray [3];

Note that myInt is a completely separate and distinct variable and any changes to it do not affect the value stored in the intArray. Think of an array variable as an address to a load of whatever type variables. Perhaps, using a warehouse analogy someArray is an aisle number. So someArray[0], someArray[1] and so on are the aisle number followed by the position number in the aisle.
[widgets_on_pages id=”udemy_advert_games_category”][widgets_on_pages id=”udemy_code_details”]

 Solving the high score problem

At the start of the tutorial we introduced a problem that arrays can solve but we never actually saw how. We can combine what we have just learned about arrays with what we already know about for loops. Take a look at this code.

// Declare and intialize an array in one step
int [] highScores; = new int [100];

// Loop through the array from 100th position [99]
// to the first [0]
// Shifting all the high scores one place
for(int i = 99; i >= 0; i--)
	// First time through the loop
	// Replace the 100th high score with the 99th
	highScores[i] = highScores[i-1];

	// Decrease the value of i and repeat for the 98th, 97th etc.

First, we declare an array called highScores to hold 100 int variables. Then in the for loop, the code loops through values of i from 99 to 0 shifting all the high scores. Three lines of code have replaced one hundred.

So what do C# arrays really do for my games?

If you think carefully about the arrays high score solution just presented, it is not as great as it first seems. The use of a for loop with arrays is common in games so was well worth looking at but what about player names who achieved the high scores? The whole thing could yet again start to get complex and sprawling. What we really need is a way to store game objects in an array. The real benefit of arrays in our game code was hinted at the start of the tutorial. Arrays can hold objects (instances of classes). Imagine that we have a ZombieEnemy class and we want to store a whole bunch of them. We could do so like this.

ZombieEnemy horde [5] {zombieEnemy1, zombieEnemy2, zombieEnemy3... etc};
The horde array now holds a load of instances of the ZombieEnemy class. Each one is a separate, living(kind of), breathing, self-determining Zombie Enemy. We could then loop through the horde array each pass through the game loop moving the zombies, checking if their heads have met with an axe or if they have managed to catch the player.

Now we have covered enough C# to play with particle systems, animate a character or build a platform game.