Billykater's unityscript tutorial: Arrays

From Unify Community Wiki
Revision as of 15:16, 25 April 2011 by Billykater (Talk | contribs)

Jump to: navigation, search



Arrays are used to group several elements together. You can for example use an array to store all sounds that need to be played in your game, All items stored in you inventory. All elements stored in an array can be accessed by their index.


Understand what the following code is doing

var itemSlotCount = 10;
var itemSlots : String[] = new String[itemSlotCount];

var itemToFind = "test";
for(var i = 0;i < itemSlotCount; i++)
   if(itemSlots[i] == itemToFind)
     print("Found item "+itemToFind);



As all other variables you have to define your arrays before using them.

var itemSlots : String[]

This will define an array itemSlots for you which consists of strings. You can also use any other type available e.g. int, GameObject, AudioClip

As you might have guessed already you can only store string values into this array. This is one of the fundamental concepts of an array. It is used to store similar items. There are ways around that but they are slower, require more code work and generally are not considered good practice. Use classes if you want to group different types together.

Beaware that the above definition just creates a variable where you can store an array, it is never actually created. Accessing it now would result in an infamous NullReferenceException. To really create an array you have two ways.

Scripting Way

itemSlots = new String[5];

This will give you an array of five strings. Just make sure the type you gave the variable matches the type after new :-)

 var itemSlots : String[] = new GameObject[5];

This is incorrect as string and GameObject don't match

Inspector Way

With unity you can directly manipulate public variables through the component inspector. Arrays can also be through the inspector if you wish.

var enemies : GameObject[];

This script attached to any game object will alllow you to choose GameObjects with all of unity's attached helpers (the search window). Just set the desired size of the array in the inspector and then edit each value.


After we have defined our array (and possibly already filled it through the inspector) we can now do something with it.


Will return the current size of it. 5 if you followed my "Scripting Way" creation.

To access the individual elements of an array you have to use their index. The index starts at 0 so an array of size 5 will have the array indices 0 to 4.


Will print the string at the second position in the array. If you fill your array through the inspector this will print the contents of Element 1. If you have followed the "Scripting Way" this will print nothing currently as we haven't put anything into the array yet.

The same way you access an element of an array you can also put something in.

itemSlots[1] = "Some awesome item";

If you now execute the print statement from before it will print "Some awesome item" into your console


Using variables instead of numbers

You never have to actually put any concrete number like 5 into the brackets.

var itemSlotCount : int = 5;
var itemSlots : String[] = new String[itemSlotCount];

This will also create an array of five strings but the 5 is taken from the itemSlotCount variable.

Another neat trick is that you can use the length of another array to create one.

var enemies : GameObject[];
var somethingRelated  : String[] = new String[enemies.Length];

This way you can define enemies inside the inspector and be sure that somethingRelated has exactly as much elements as enemies without you changing it each time you change enemies in the inspector.

Not only the creation can take variables also accessing the elments can be done through variables. One of the most common use cases will be to iterate through all elements

for(var i = 0;i < itemSlots.Length; i++)

This will print all your variables to the debug console.

for(var i = 0;i < itemSlots.Length; i++)
   itemSlots[i] = "MyItem "+i;

This will set the elements of the array to

Index String
0 MyItem 0
1 MyItem 1
2 MyItem 2

if the array has the size three.

Adding new elements/Resizing

An array always has a fixed length. So after creating an array of 5 elements you can't add a sixth. There are serveral ways around that.

Creating a new one and copy all elements

var oldArray : String[] = new String[3];
var newArray : String[];

newArray = new String[oldArray.Length + 1];
for(var i = 0;i < oldArray.Length; i++)
   newArray[i] = oldArray[i];
oldArray = newArray;

Using the builtin functionality that does exactly execute the former code

oldArray.Resize.<String>(oldArray , 5);
System.Array.Resize.<String>(oldArray, oldArray.Length + 1);

Array of classes

There is a small difference when using arrays with your custom classes and nonprimitive types (e.g. string, int, float). As these are reference types the array only contains a reference to them.

class Enemy
   var health : int;
   var prefab : String;
var enemies : Enemy[] = new Enemy[5];

This will only create an array that can store 5 enemies it doesn't create 5 enemies. So using this code will result in a NullReferenceException. There simply are no enemies.

enemies[0].health = 5;

You have to create them yourself. A straightforward way to do this might be

for(var i = 0;i < enemies.Length; i++)
    enemies[i] = new Enemy();


This section is reserverd for anyone who might post their example code which they came up with after reading this tutorial. It should show different usages of arrays. Please don't post the same code over and over and at least write a description to each of your examples.

The end

Now you can hopefully figure out what the script in the "Scope section" is doing.

If you want some more information about what functions you can call for an array search for system.array in the msdn Direct link

Personal tools