Accessing enumeration values by their index in JS?

Hi everyone,

I'm wondering if there is a way to access an enumeration value by it's index? I'm trying to iteratively populate a hashtable where the keys are enumeration values and the hashtable values are prefab objects. Something like this:

enum VehicleType {car, boat, airplane, motorcycle};

var vehiclePrefabs : Transform[];
private var vehicleModels : Hashtable;

function Start ()
{
     vehicleModels = new Hashtable();
     for (i = 0; i < vehiclePrefabs.length; i++)
     {
          vehicleModels.Add(VehicleType_, vehiclePrefabs*);*_
 _*}*_
_*}*_
_*```*_
_*<p>This is so ultimately I can do this:</p>*_
_*```*_
_*function SpawnVehicle(vehicleType : VehicleType, spawnPoint : Vector3)*_
_*{*_
 _*var spawnVehicle = Instantiate(vehicleModels[vehicleType], spawnPoint, Quaternion.identity);*_
_*}*_
_*```*_
<em>_<p>Now when I try to access the value by saying `VehicleType*` I get an error about "Type 'System.Type' does not support slicing."</p>*_</em>
<em>_*<p>I searched the forum a bit and found a post that mentions using `Enum.Getnames(VehicleType)`, but that seems to get all the names at once, and I want them one at a time.  And I can't seem to find anywhere in the Scripting Reference that adequately describes what you can do with `Enum.`.</p>*_</em>
<em>_*<p>Any answers or clarification in what I'm doing wrong and what it's possible to do in terms of manipulating enumerations would be appreciated!</p>*_</em>
<em>_*<p>-Dylan</p>*_</em>

An enum type can be implicitly cast to an integer. So you can access your vehicleModel array by writing vehicleModels[VehicleType.car];

Enums are described in the .net docs; they're not Unity-specific so they won't be documented in the Unity manual. However, I don't see a need for a hashtable here. It will work fine like this:

enum VehicleType {car, boat, airplane, motorcycle};

var vehiclePrefabs : Transform[];

function SpawnVehicle(vehicleType : VehicleType, spawnPoint : Vector3)
{
     var spawnVehicle = Instantiate(vehiclePrefabs[vehicleType], spawnPoint, Quaternion.identity);
}

Then you can do

SpawnVehicle(VehicleType.boat, Vector3.zero);

or whatever. Enums are just ints which are referred to by name, not strings. Though you can substitute the actual value if you want. i.e.,

SpawnVehicle(1, Vector3.zero);

Note that you can explicitly define the values:

enum VehicleType {car = 0, boat = 1, airplane = 2, motorcycle = 3};

You can use whatever numbers you want (as long as they are positive integers); they don't have to be in order.