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
0
Question by Yharooer · Jul 11, 2013 at 11:45 AM · javascriptvariableenumsyntax

How to use Enum?

Hi all,

I've been searching around, and I haven't been able to find any guides on using enums in Unity.

I was wondering if someone could show me how to use them in an 'if' statement.

This is my code so far:

 #pragma strict
 var moveSpeed = 5;
 enum Direction {up,down,left,right,none};
 var moveDirection : Direction;
 
 function Update () {
     if (moveDirection = moveDirection.up) {
         transform.eulerAngles = Vector3.zero;
         MoveForward();
     }
     if (moveDirection = moveDirection.down) {
         transform.eulerAngles = Vector3.zero;
         transform.rotation.z = 90;
         MoveForward();
     }
     if (moveDirection = moveDirection.left) {
         transform.eulerAngles = Vector3.zero;
         transform.rotation.z = 180;
         MoveForward();
     }
     if (moveDirection = moveDirection.right) {
         transform.eulerAngles = Vector3.zero;
         transform.rotation.z = 270;
         MoveForward();
     }
 }
 
 function MoveForward () {
     transform.Translate(Vector3.forward);
 }

And I get 3 errors:

 Assets/Scripts/MyGameScripts/Gameplay/Movement/PlayerMovementController.js(22,27): BCE0044: expecting ), found '='.
 
 Assets/Scripts/MyGameScripts/Gameplay/Movement/PlayerMovementController.js(22,45): BCE0043: Unexpected token: ).
 
 Assets/Scripts/MyGameScripts/Gameplay/Movement/PlayerMovementController.js(23,39): BCE0044: expecting :, found '='.

How can I fix this, and so you know any guides on using enums? Thanks!

Comment
Add comment · Show 5
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 fafase · Jul 11, 2013 at 12:14 PM 1
Share

By the way why not using Vector3?

 var moveDirection :Vector3;
 
 if(moveDirection == Vector3.right){}
 if(moveDirection == Vector3.up){}
 // and so on
avatar image moonstruck · Jul 11, 2013 at 12:22 PM 0
Share

The problem with this approach is that moveDirection won't be exactly equal to right or up (depends on how it is calculated). So if you want to use something like this, then the workable solution is if( ( moveDirection - Vector3.right ).sqr$$anonymous$$agnitude < threshold ) {} with some low threshold like 0.1f. Though it is worse performance-wise because you have to compare vectors and not just int's.

avatar image fafase · Jul 11, 2013 at 12:27 PM 1
Share

It would depend how the value is passed. Since he uses the enum it is either right or left or forward or ...but never in between. So it means he would passing:

moveDirection = Vector3.up;

So there is no inacuracy in that case

avatar image moonstruck · Jul 11, 2013 at 12:32 PM 1
Share

You can't compare the enum value (i.e. int) to the Vector3, the compiler will report a syntax error.

avatar image fafase · Jul 11, 2013 at 12:36 PM 1
Share

Ok one last time and then I stop,

 var moveDirection :Vector3;
  
 if(moveDirection == Vector3.right){}
 if(moveDirection == Vector3.up){}
 // and so on

moveDirection is a Vector3. But ok I give you credit, enum is faster.

1 Reply

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

Answer by moonstruck · Jul 11, 2013 at 11:58 AM

First of all, if you want to compare then you should use operator == and NOT the assignment operator. Then the enum values don't belong to the variable, but to the object. So you can rewrite it as following:

 if (moveDirection == Direction.up) {
     // ...
 }
Comment
Add comment · Show 1 · 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 Yharooer · Jul 11, 2013 at 12:03 PM 0
Share

Oh right... oops... Thanks!

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

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

Change variable value of a script from another script. both in different Game object 1 Answer

Countdown code not working 1 Answer

On Button press Move Value toward 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