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 Android Matt · Jun 22, 2013 at 07:27 PM · getcomponentif-statementsenum

Neater way to test an Enum in a different script?

I have a script terrainInfoJS.js that stores various bits of information about each type of terrain in my game. I have another script unitInfoJS.js that stores info related to the armies/units that populate the map. The terrainInfoJS.js has a series of enums for defining various aspects of the terrain e.g:

 enum TerrainType {
     unassigned = 0, 
     grass       = 1,
     hills       = 2,
     mountain   = 3}
 
  //initialising the enum
 var terrainType = TerrainType.unassigned;

The unitInfoJS.js script now needs to test this enumeration so that the correct actions are performed depending on the terrain that the unit occupies. I can't get this to work in a succinct way with lettered variables, only with numbered. For example, this works:

 //if terrain is hills
 if(terrainInfoJS.terrainType == 2){ }

..but this does not work:

 //if terrain is hills
 if(terrainInfoJS.terrainType == hills){ }

...and neither does this:

 //if terrain is hills
 if(terrainInfoJS.terrainType == terrainType.hills){ } 

...but this seems to work:

 //if terrain is hills
 if(terrainInfoJS.terrainType == terrainInfoJS.terrainType.hills){ }

However, only the first way is elegant, but that's not very readable out of context due to the arbitrary numbers from the enum. The second, and third don't work, and the last one seems pretty clunky. Is there a way to set up an Enum so that it can be tested from another script in a readable way with minimal extra code?

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

1 Reply

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

Answer by Dave-Carlile · Jun 22, 2013 at 07:47 PM

I believe you want to compare to TerrainType.hills, not terrainType.hills. TerrainType (upper case initial t) refers to the enum, terrainType (lower case initial t) referes to your variable. Case matters.

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 Android Matt · Jun 23, 2013 at 12:30 AM 0
Share

Thanks! It all works as intended now.

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

C# GetComponent Issue 2 Answers

Accessing Enumeration from another script issue 1 Answer

Simple question about OnMouseButtonAsUp 1 Answer

Type of a script 1 Answer

Reference Not Set toInstance of Object 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