Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 NewfieJoe · May 20, 2011 at 09:32 PM · javascriptvariableclassenum

Enum style variable

How can I create a variable that looks like an enum in the inspector? Do I have to declare it as a class? IE

 public class myEnumvar {
   var item1: int = 1;
   var item2: int = 2;
 }
 public var myEditableEnum: myEnumvar;

Or is there a more straightforward way of doing it?

Comment
Add comment · Show 1
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 NewfieJoe · May 20, 2011 at 09:47 PM 0
Share

for anyone wanting this functionality the above code works btw. I just wondered if there was a syntax for a more intuitive way of declaring it similar to: public enum var myvar { var1:int, var2:int } or if I was already doing it correctly and to leave it as-is

2 Replies

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

Answer by sneftel · May 20, 2011 at 10:12 PM

What an enum is, is a set of non-editable named values. You can then make variables of that enum type, and those variables will be settable to any of the named values (with a useful dropdown box).

It sounds like you don't actually mean "enum". Do you just want to organize a set of variables into its own scope? If so, the code you're using above is the most straightforward way of doing it. You shouldn't call it "enum", though, as that's an unrelated concept.

Comment
Add comment · Show 5 · 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 NewfieJoe · May 20, 2011 at 11:24 PM 0
Share

Actually I didn't. :) "a variable that looks like an enum in the inspector?" I thought scope mean public, private or static. (I'm self taught) Not entirely familiar with the correct ter$$anonymous$$ology. Just wanted to make sure I was doing it right, which I take it I am. Thanks. :)

avatar image NewfieJoe · May 20, 2011 at 11:32 PM 0
Share

I also wanted to set some of them with labels ins$$anonymous$$d of numbers so they could be selected from a dropdown list. Right now I just use an enum for the list of labels. Then a separate editable group of variables with "Label" entries and IDs that match the enum dropdown which seems clunky.

avatar image sneftel · May 23, 2011 at 01:38 PM 0
Share

"Scope" is one of those things that programmers use to mean way too many different things. :-) The more unambiguous term for private/public/static is "protection level", but I've definitely seen people call it "scope".

avatar image sneftel · May 23, 2011 at 01:38 PM 1
Share

If you want to use an enum-style dropdown box to select a set of values, you have a couple of options. The first is, as you say, use an enum for the dropdown, then translate from that to what you actually want.

The other way is to move to CustomEditor. These can be time-consu$$anonymous$$g to write and keep up to date, but they're the only way to have full control over the inspector.

avatar image Joshua · May 23, 2011 at 01:44 PM 0
Share

Custom editors (or often more practical/easier: custom inspectors) are definitely worth the effort if you're working on a complicated script.

avatar image
0

Answer by sneftel · May 20, 2011 at 09:42 PM

Like this.

Comment
Add comment · 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 NewfieJoe · May 20, 2011 at 10:05 PM 0
Share

No that just declares enumerator constants that can't be edited. I need something that is visible in the editor and can be edited during runtime. So it looks like an enumerator but is read/write ins$$anonymous$$d of read only. Just for legibility for other $$anonymous$$m members who don't read code very well.

avatar image NewfieJoe · May 20, 2011 at 10:05 PM 0
Share

So I can use something like if( player.health > player.maxhealth ). It also lets me organize my variables into sub-groups. $$anonymous$$y main routine has over 100 variables, if they were one long list it would be incomprehensible.

Together this makes variables human readable and organized and makes my code read like pseudocode.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Is it possible to create a script dinamic like animation>size controling the Elements variables? 1 Answer

How to Typecast JS Variables as C# Classes? 0 Answers

Different way to access class variables? 1 Answer

Finding the length of an enum in a different class 1 Answer

Creating A Class Variable That Works With Any Class 0 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