• 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
Question by Mattivc · Apr 16, 2010 at 10:51 AM · aienumstatejavascript-specificienumerable

How to declare and use a enum variable in Javascript?

What is the correct way to declare and use a enum variable, in Javascript?

Comment
duck
runevision
Novodantis 1

dhendrix
Jean-Fabre
kovpas
Olie
Matthew A
Jesus_Freak
Kourosh
T.
Joshua
SisterKy
Rennat
And 15 more...

People who like this

30 Show 6
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 FalconRime · Apr 13, 2011 at 01:49 PM 0
Share

Can you comment on whether (1) the enum declaration and (2) the use of the variable, has to be in the (a) the same script, (b) a script on the same object or (c) doesn't matter?

avatar image Toxic Blob · Apr 13, 2011 at 02:05 PM 3
Share

To keep things organised I have all my enums in their own scripts in a separate folder (Scripts/Enums). If your enums are within a class you'll have to specify the class prior to the enum to access it, which is often needlessly awkward.

avatar image spacepilot · Jan 06, 2012 at 09:50 PM 0
Share

Is enum limited to integer-values? This site here states that the sub-vars of an enum-declaration are always integer. I need float - is there a way?

avatar image aviose · May 04, 2012 at 08:49 PM 1
Share

Positive integers, actually... it's one of the limitations of enums.

avatar image Bunny83 · May 04, 2012 at 09:19 PM 0
Share

Exactly. If you use the enum as a variable type, the variable is actually just an int32 behind the scenes. This is a fix behaviour and can't be changed.

You can use an enum + an array with float values and use the enum constants as index into the array to get the float value.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by duck · Apr 16, 2010 at 11:27 AM

Like this (using the example of various AI states for an AI State Machine):

enum AIState { Asleep, Idling, Chasing, Fleeing, HavingLunch }

And then you can assign any value from that enum to a variable, like this:

var state = AIState.Idling;

In the above example, the variable's type is implicitly defined as "AIState". You can explicitly define a variable with that type like this:

var state : AIState;

And similarly you can define the input parameter of a function to recieve a value from your enum, like this:

function PerformAction( currentState : AIState ) {

 switch (currentState) {
     case AIState.Asleep: 
         Debug.Log("Zzzz.");
         break;

     // etc...
 }

}

Comment
Mattivc
spinaljack
Molix
Horsman
Novodantis 1
Fuller

dhendrix
Whimsical
Sebas
Jean-Fabre
maikkel
Olie
PeterDC
Matthew A
And 46 more...

People who like this

61 Show 3 · 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 · Jun 15, 2010 at 08:55 AM 1
Share

I've upvoted your answer because it helped me, but your example code is slightly incorrect. In the enum declaration, you've named it 'Asleep', but in the switch, you're checking for 'Sleeping'. Cheers.

avatar image duck ♦♦ · Jun 15, 2010 at 10:10 PM 0
Share

thanks for the pointer! just edited it to fix that.

avatar image RoxPhilosopher · May 18, 2011 at 11:51 PM 0
Share

Is it true that enum declarations have to be outside of a function?

avatar image

Answer by Toxic Blob · Apr 13, 2011 at 02:03 PM

You can also assign values to your individual enum elements.

enum Layer
    {
    Protagonists    = 9,
    Antagonists     = 10,
    HeadsUpDisplay  = 11,
    LevelBoundaries = 13
    }

This is incredibly helpful if you use layers, for example. Rather than using

this.gameObject.layer = 9;

You could instead use the easier to read

this.gameObject.layer = Layer.Protagonists;

This is especially helpful as there is then only one location, your enum, where you have to change the layer number if a change is needed.

Comment
efge
Deozaan
Joshua
RoxPhilosopher
dissidently
xortrox
Billamu
jonas.du
jahroy
Somian
stixNstonez
cupsster
nuverian
KiraSensei
GoCatGoGamesLLC
And 6 more...

People who like this

21 Show 2 · 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 sabliao · Oct 12, 2011 at 11:00 PM 0
Share

What would be the purpose of assigning them different layers? Is it one way to have another kind of differentiation? Such as only finding objects within a certain layer?

avatar image andyisbonza · Mar 30, 2014 at 06:52 AM 0
Share

That is a great tip - upvoted!

I use layers to stop objects from colliding with each other when I don't want them interacting. If, for example, you had an enemy that was unaffected by bullets, one option would be to put them on a different layer from each other, then ensure that the bullet's layer and the enemy's layer didn't cause collision (under Project settings --> Physics)

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Field of view, using raycasting 5 Answers

Having an Error with An Enum : error CS0176: Static member `BossOne.BossActionType.Attacking' cannot be accessed with an instance reference, qualify it with a type name instead 2 Answers

Calling a function outside the Finite State Machine 0 Answers

GOAP - how can you change the state outside of an action? 1 Answer

Can you end a coroutine from outside of that coroutine? 2 Answers


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