- Home /
Question by
CameronSanders · Aug 08, 2013 at 06:44 AM ·
javascriptif statement
Why does my if statement skip its body?
My second if statement doesn't seem to want to read its body. it sends the debug, but everything else is skipped. can anyone explain why? lines 19,20, and 21 never happen. no errors in debug.
#pragma strict
var switchup = false;
var lever : Transform;
var distance = 0.0;
var vision = 5.0;
var player : Transform;
function Start () {
}
function Update () {
var click = Input.GetMouseButtonDown(0);
distance = Vector3.Distance(lever.position, player.position);
if (distance < vision && click) {
if (switchup == false) {
Debug.Log("has passed test, is false");
switchup = true;
lever.localPosition = Vector3(-0.01412122,0,0.3352401);
lever.localRotation = Quaternion.Euler(0,47.94865,0);
}
else
Debug.Log("reset to starting pos");
switchup = false;
lever.localPosition = Vector3(0.7541609,0,-0.05813695);
lever.localRotation = Quaternion.Euler(0,310.3725,0);
}
}
Comment
Best Answer
Answer by robertbu · Aug 08, 2013 at 06:48 AM
You don't have any '{}' in your else statement, so lines 25-27 get executed regardless of the value of switchup. Only the 'Debug.Log()' statment is contingent on the 'else.' The result is that switchup gets set to false immediately after it has been set to true.
@CameronSanders : you should accept the answer by clicking the (V) below the thumbs-up and thumbs-down, so as to close the question.