# How to make my condition works well?

i have this condition, but only the first condition works and i dont know why.
Someone can help me?
Thanks.

public bool passed;

``````    void Update(){

if (transform.position.y >= -4 && !passed) {

Debug.Log ("works123");
passed = true ;
}

if (transform.position.y >= 5 && !passed) {

Debug.Log ("works");
passed = true ;
}
``````

}

If transform.position.y is greater than 5, then it is ALSO greater than -4 (since 5 > -4 and greater-than is transitive). This means it will enter your first condition EVERY TIME.

Since in your first IF you set passed to TRUE, it therefore will not enter the second IF, which requires passed to be FALSE.

I suspect you may mean <= -4 instead of >= , but if not, you should probably flip their order:

`````` if (transform.position.y >= 5 && !passed) {

Debug.Log ("works");
passed = true ;
}

if (transform.position.y >= -4 && !passed) {

Debug.Log ("works123");
passed = true ;
}
``````

In this case, numbers larger than 5 will enter that conditional FIRST, and therefore set passed there instead of the other, more general case. You could also make it slightly easier to understand if you used an ELSE condition.

EDIT:

OK, I think I understand your goal a little better now. You want to write each condition exactly once, on the frame where they pass the y-position given. There are a couple of reasons your original code didn’t work; not resetting the boolean “passed” was a big one.

``````Vector3 lastPosition;

void Start()
{
// ...  other code here ...
lastPosition = this.transform.position;
}

void Update()
{
if (lastPostion.y < 5 && transform.position.y >= 5)
{
Debug.Log("Passed 5");
}
else if (lastPosition.y < -4 && transform.position.y >= -4)
{
Debug.Log("Passed -4");
}

lastPosition = transform.position;
}
``````