Using Arrays Properly in Unity3d

I’m having trouble doing something that should be very simple and thus I’m here.
What I’m trying to accomplish is create an array of booleans values.

For instance status ailments

However every attempt to do so results in errors from the compiler.


var Resistances=new Array();//a list of all current resistances.
var Resistances[“Weak”]=0;
var Resistances[“Resist”]=0;
var Resistances[“Wound”]=0;
var Resistances[“Paralysis”]=0;


Any help would be greatly appreciated in solving this problem.

What you need is a Dictionary or Hashtable also known as associative array.

That’s actually no arrays in the traditional sense. Keep in mind you’re not using JScript in Unity. UnityScript is also a compiled language. Dynamic objects are not supported my .NET / Mono.

In your case i would use a Dictionary. You need to include the “System.Collections.Generic” namespace to use it.

import System.Collections.Generic;

// ...
var Resistances = new Dictionary.<String,boolean>();

The values are not auto generated, but once they are added you can use them like an associative array:

Resistances["Weak"] = true;

Have a look at the scripting reference. You can use arr.Push(object) to add a value to the array. Btw you say boolean… You should use true and false then instead of 1 and 0. Also, leave out the “var” when assigning values.

Wiki-post I found very helpful when trying to decide which approach (array, list, dictionary) to use:

Greetz, Ky.