- Home /
Mouselook C#, which if/else statement?
Hi, so I'm studying the default MouseLook script and while I've made progress I do need to know how the 'compiler'(or something) cycles through the code.
So, at the start we create an enumeration and then we set an instance of it equal to one of that enum's members:
public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
public RotationAxes axes = RotationAxes.MouseXAndY;
So, for the rest of the script axes' value is never changed. Which should surely mean that it will always == RotationAxis.MouseXAndY.
As shown in the code snippet which pretty much directly follows within Update():
if (axes == RotationAxes.MouseXAndY)
So the 'compiler' executes the code here because it must be true. Yet, later on we get other if and else statements claiming 'axes' to equal other values not obviously assigned to it.
Here is what I mean:
else if (axes == RotationAxes.MouseX)
axes should still equal RotationAxes.MouseXAndY, shouldn't it? Then this code would never be executed, but clearly it must be executed or it wouldn't be there.
So how does this work. 'axes' can seemingly equal MouseXAndY and then MouseX, I get that it's a variable and so its value is subject to change but surely that change should be stated in the form of:
axes = something.
Yet the only time such an initialization occurs is at the start when it's set equal to 'MouseXAndY'.
Perhaps I've misunderstood how enums work.
If you can help that would be greatly appreciated and please let me know if I've been unclear.
Answer by Ernst · Oct 19, 2014 at 10:59 PM
The variable RotationAxes axes is set to public. Because of this, you can set it to MouseXAndY, MouseX or MouseY in the inspector. When you set in in the inspector for example to MouseX, the else if(axes == RotationAxes.MouseX) part will be executed.
Your answer
Follow this Question
Related Questions
Use Enum of another script in an if statement C# 2 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
If statement not working correctly. 3 Answers
Problems with enums and switches (C#) 2 Answers