Need help translating MouseLook.cs -- new to C# and enums

Hey guys… I’ve been working in javascript, so I’m completely new to C#. I have a straight-up Noob question about C# and enums.

I’m trying to decipher the sample mouselook.cs script, so that I can cut it up and make it work for a networked game. here’s what’s confusing me, and how I’m interpreting it:

first we declare some variables, apparently:

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

so now we’ve got an enum with 3 values called RotationAxes. that seccond line is confusing. it looks like it’s creating a SECOND variable called axes, which is of type “RotationAxes” – so an enum as well… but it’s only grabbing one value – MouseXAndY – 0. so why not use an int?

so in my head, we’ve got 2 varaibles. one is an enumerator with 3 values, and one is… an enumerator?? with one value? a duplicate of RotationAxes, but with only one of it’s values? crazy.

now down below, we’ve got a bunch of conditional if statements, asking for different values of ‘axes’:

void Update ()
{
if (axes == RotationAxes.MouseXAndY){
{
.
.
.
else if (axes == RotationAxes.MouseX){
.
.
.
else{

see – in my head, that’s impossible. It seems that axes was given a specific value, and it’s not being changed anywhere else in the script… so how exactly is anything but the first conditional statement ever being called?

I’m sure there’s some sort of process that i completely don’t know about.

Anyone care to enlighten me?

Many thanks.

No, the first statement does not create a variable. It defines an enum, much like you would define a class and then later use it to instantiate an object of such class.

Enums are used to make code more clear instead of having to use numbers which mean nothing to programmer. The computer will use the integers for its computations.

Because then you are using magic numbers, which are bad. Enums are primarily a compiler thing, to make scripts easier to read, so you’re not just using random arbitrary magic numbers for things. (Even though you really are, but it’s hidden behind words instead.) Maybe it’s more clear in Javascript:

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

–Eric

ah, wonderful. thanks for the replies guys.

that does help a lot, but I’m still very confused on the last part of the script…

so we made an enumerated type and then created a variable of that type… we then assigned a value to that variable of the first enumerator in the type – that is specifically, MouseXAndY – or “0”.

HOWEVER-- that still doesn’t explain how the other if statements could ever be called.

the value of axis doesn’t seem to be changed anywhere else in the script… unless ‘axis’ is some sort of global variable that’s being changed somewhere else or something?

right now it seems like axis is being set to 0, and there’s pretty much no way that the second and third if statements could ever be called.

Thanks again for you help so far…

It’s changed by the user in the Inspector. Public variables in scripts are always overridden by whatever’s set in the Inspector. You could also change it from another script, or add stuff to the current script that changes it based on some conditions.

–Eric

AH! i get it now.

so it’s basically set up this way so that you can chose to have vertical only movement, or horizontal only movement.

duh. okay.

Thanks very much!!