Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by Dylan Cristy · Oct 31, 2010 at 11:41 PM · javascriptenumindexbce0048

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[i], vehiclePrefabs[i]); } }

This is so ultimately I can do this:

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

Now when I try to access the value by saying VehicleType[i] I get an error about "Type 'System.Type' does not support slicing."

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..

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!

-Dylan

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Eric5h5 · Nov 01, 2010 at 12:31 AM

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.

Comment
Add comment · Show 8 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Dylan Cristy · Nov 01, 2010 at 06:17 PM 0
Share

Ah, gotcha. I thought I had to go through an extra step to associate the enum names with the prefabs I want them to refer to. But I totally see how this works. $$anonymous$$uch easier. Thanks!

avatar image aviose · Apr 22, 2012 at 02:44 AM 0
Share

So you can't use a negative number in an enumeration in Unityscript? I've seen it done in C#, so it seemed odd to me. Both are capable of the same in $$anonymous$$OST cases.

avatar image AlucardJay · Jan 09, 2013 at 01:17 AM 0
Share

I'm having trouble understanding this . I have written (uJS) :

 for ( var i : int = 0; i < System.Enum.GetValues( AttributeName ).Length; i ++ )
 {
     var enumValue : AttributeName = i;
     GUI.Label( Rect( 10, i * 40, 100, 25 ), enumValue.ToString );
 }

but this throws the error : BCE0023: No appropriate version of 'UnityEngine.GUI.Label' for the argument list '(UnityEngine.Rect, function(): String)' was found.

it seems for C# this can be done with :

     GUI.Label( Rect( 10, i * 40, 100, 25 ), ((AttributeName)i).ToString );

where am I going wrong? Thanks.

avatar image fafase · Jan 09, 2013 at 01:38 AM 1
Share

I think

 var enumValue : AttributeName = i;

should be

 var enumValue : AttributeName = (AttributeName)i;

What you are trying to do, assigning an int to an enum, works with C but was dropped in C++, C#.

I would think that is one of the drawback of UnityScript, C# would tell you wrong and you would not pass compilation.

avatar image AlucardJay · Jan 09, 2013 at 01:53 AM 0
Share

Thanks, unfortunately this throws the error : UCE0001: ';' expected. Insert a semicolon at the end.

What I'm trying to achieve is to iterate through all the values of the enum, and display them (as a string), eg :

 public enum AttributeName
 {
     $$anonymous$$ight, 
     Constitution, 
     Nimbleness, 
     Speed, 
     Concentration, 
     Willpower, 
     Charisma
 }

I assumed enums worked both ways, so if I specified the index of the enum, it would return the value, eg 0 returns $$anonymous$$ight, 3 returns Speed, etc. Just as parseInt( AttributeName.$$anonymous$$ight ) works the other way.

This is what I found before writing the above code : http://answers.unity3d.com/questions/42743/integer-value-casting-to-keycode-enum-in-js.html

Show more comments
avatar image
4

Answer by StephanK · Nov 01, 2010 at 12:32 AM

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Insert a string to a list inside a list 3 Answers

Making a user-friendly array with indices based on an enum 2 Answers

Javascript array: Negative Indexes? 2 Answers

[CLOSED] UnityScript - enum to numeric value error 1 Answer

Javascript Enum Question 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges