- Home /
C# Boolean Constantly Changing
I am trying to alter a script that is not mine, but my boolean variable dead is not working correctly. The function CameraDeath() is called from a different script and correctly runs a single time, and the Debug.Log in that function prints only once proving that. However, after that function is called, my boolean constantly switches between true and false. The Debug.Log constantly running in the Update function goes back and forth, printing true and false for the value of dead. I have absolutely no idea why. I hope you can help.
public class MouseLook : MonoBehaviour {
public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
public RotationAxes axes = RotationAxes.MouseXAndY;
public float sensitivityX = 15F;
public float sensitivityY = 15F;
public float minimumX = -360F;
public float maximumX = 360F;
public float minimumY = -60F;
public float maximumY = 60F;
public bool dead = false;
public bool deadFunctionRan = false;
float rotationY = 0F;
void Update (){
Debug.Log(dead);
if (axes == RotationAxes.MouseXAndY && !dead) {
float rotationX = transform.localEulerAngles.y + Input.GetAxis ("Mouse X") * sensitivityX;
rotationY += Input.GetAxis ("Mouse Y") * sensitivityY;
rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
transform.localEulerAngles = new Vector3 (-rotationY, rotationX, 0);
} else if (axes == RotationAxes.MouseX && !dead) {
transform.Rotate (0, Input.GetAxis ("Mouse X") * sensitivityX, 0);
} else if(!dead) {
rotationY += Input.GetAxis ("Mouse Y") * sensitivityY;
rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
transform.localEulerAngles = new Vector3 (-rotationY, transform.localEulerAngles.y, 0);
}
}
void CameraDeath(){
dead = true;
Debug.Log("function ran");
}
void Start (){
// Make the rigid body not change rotation
//if (rigidbody)
// rigidbody.freezeRotation = true;
}
}
Is the "dead" variable accessed anywhere outside of the class?
If so, please post the class that is accessing it.
Yes, you could start by making dead private.
The compiler will then give you an error everywhere else that is accessing it.
No, dead is only being accessed inside the script posted and privatizing the variable gives no errors at compilation or at runtime.
$$anonymous$$attyman's comment is correct. The script was attached to two objects. Unfortunately, I can't change this, so a different solution to my problem is going to have to be devised. Thank you.
Answer by mattyman174 · May 06, 2014 at 02:25 AM
Do you possibly have 2 MouseLook scripts attached to a GameObject or on any other GameObject? This could cause that type of output where one has called the CameraDeath() Function and its instance of the dead variable is set to true whilst the other still remains false.
It may look as though your bool is switching, but its just outputting the different values to the console.
Can you accept this as correct to close this Question. Thanks.
Your answer
Follow this Question
Related Questions
GetComponent, set boolean to true but it willn't revert 3 Answers
Need help accessing booleans from other script. 2 Answers
c# Ignoring conditional statement? 1 Answer
What does this mean? 1 Answer