Exposing variables in the inspector JS

from the c# mouselook script…

	public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
	public RotationAxes axes = RotationAxes.MouseXAndY;

this gives you a pull down in the inspector and initially sets it to MouseXandY. any pointers on doing this in javascript?

i found an older post (last april) that says you have to use enums through c# but that javascript would be supported in the next major release. did that make 1.6?

If you used to need to use enums in C#, you don’t now. I use JS enums a lot.

enum Mode {Menu, Construction, Arena}
var CurrentMode : Mode = Mode.Menu;

There are some limitations to this - for example, arrays of enums will not show up in the inspector like, say, an array of strings - but that’s the jist of it.

If you see an integer displayed in the inspector, first make sure you’ve explicitly declared the type of the variable ( : Mode, or whatever your enum type is called) and if that doesn’t work, restarting Unity fixes everything :wink:

i’m betting i didn’t declare the type correctly. i’ll try it again. thanks!

works like a charm and indeed it didn’t at first. i had it declared correctly but it was a relaunch that got it working. gotta remember that one. thanks again!

Actually you only have to deselect the object in the inspector and select it again. (Right now Unity doesn’t auto update the inspector when a type changes from int to enum, since it internally is treated in the same way.)

well hey, that’s better than a relaunch… thanks for the info!

A bit OT, but could someone explain the advantage of using Enums over Classes?

I understand that it is handy for encapsulating data, however, this might as well be done by defining a new class. Is this a performance issue?

Enums are used to give names to a set of constants. So, for example, defining enum WindowState {Open, Closed}; would allow you to have variables of type WindowState which can be assigned the values WindowState.Open or WindowState.Closed. That’s generally preferable to having an int or string variable to which you assign arbitrary values, as it makes it harder to accidentally use a value which isn’t valid.

It has nothing to do with data encapsulation or efficiency. It’s solely to do with putting human-readable names onto values, and making sure you can only use them as intended.

Well I guess in your example it would be easier to use 1 boolean, especially as a window can’t be closed and open at the same time, but I think I know what you mean.

Sure, but it might be clearer to label it with something which explains what it does when it’s used as a parameter to a function. For example, see SendMessageOptions, used as a parameter to SendMessage. If SendMessage took a bool as its last parameter, it’d be much more difficult to tell what it does at a glance.

And if you have three or more values, it’s obviously even more meaningful. :slight_smile:

when i couldn’t get it working i had it as an int. in the inspector i had to enter 0, 1 or 2 depending on what i wanted my script to do. someone else would have to look at my script and hope i commented.

instead with enums, i now have a pull down in the inspector and i can select MouseXandY, MouseX or MouseY.

Oh, I forgot that you only store Constants there. Then my last post is total rubbish.

Now I’ve gotcha. Thanks!