- Home /
if statement in a get function
So I'm working on a project with a camera where i have the camera but other team members don't so i have 2 different object groups 1 virtual and 1 that works with the camera. So I'm trying t make a index script that checks what your using and grabs all the info needed so are you using camera? grab fov from the camera, are you using virtual? grab fov virtual and set them as a get function so what i tried was this:
public static float get_FOV(){
if (virtuEnabled) {
return CameraCapture.getfovY;
} else {
return MouseLook.getFOV;
}
}
i also tried it like this:
public static float get_FOV {
get {
if (virtuEnabled) {
return CameraCapture.getfovY;
} else {
return MouseLook.getFOV;
}
}
}
the virtuEnable is a bool set true or false depending what your using. now the problem that im having is that it gives me this error Assets/Scripts/varLibrary.cs(47,7): error CS0120: An object reference is required to access non-static member `varLibrary.virtuEnabled' line 74 is refering to the if (virtuEnabled) {
any help in this matter would help alot!
Answer by tanoshimi · Jul 24, 2017 at 09:26 AM
That's because you've declared the get_FOV function static
, but the virtuEnabled variable it tries to access is not.
If this is meant to be a single, shared, class-level function, use the static
modifier with both the get_FOV function and the virtuEnabled variable declaration. Alternatively, if this is meant to be an instance function, remove the static
modifier from both.
On all my previous public float get function it never wanted to work without static but i tried
public float get_FOV { get { if (virtuEnabled) { return CameraCapture.getfovY; } else { return $$anonymous$$ouseLook.getFOV; }
} }
and somehow it works thanks a lot!
Your answer
Follow this Question
Related Questions
operator && cannot be applied to operands of type bool and method group 1 Answer
Store a reference to an object with a certain boolean? 0 Answers
how to stop the player when gameover?? 1 Answer
Unity plays the animation then jumps to another, then back again. 0 Answers
Double collision on "ground" tagged object results in player thinking they're in air? 1 Answer