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
Poison=1
Paralysis=0
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>();
Resistances.Add("Weak",false);
Resistances.Add("Resist",false);
Resistances.Add("Wound",false);
Resistances.Add("Paralysis",false);
The values are not auto generated, but once they are added you can use them like an associative array:
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.