Doesn't check both if statements conditions

Hey there!

I’ve got a problem with an if statement in an if statement.
The code in the inner if statement should start execute when the two if statements are true, right? ( doesn’t work either)
In my case, when they are false. Why doesn’t it work in my script below?
When only one condition is false, the while loop gets executed!
I did a lot of testing, and I’m sure the two variables are doing what they have to do. (They are false when I want them to be false and vice versa).
Does anyone know the problem here?
Please, help me out :smile:

	distance = Vector3.Distance(transform.position, boxPuzzlePosition2.transform.position);
	if(!boxFinished){//this condition...
		if(!targetScript2.pushed){//...and this condition need be false to execute the while loop
			while(distance > 0){
		       transform.position = Vector3.Lerp(transform.position, boxPuzzlePosition2.transform.position, Time.deltaTime * 0.85);
		       distance = Vector3.Distance(transform.position, boxPuzzlePosition2.transform.position);
		       yield; //yielding to this just means it won't just reach the destination instantly
		   }
		}
	}

    distance = Vector3.Distance(transform.position, boxPuzzlePosition3.transform.position);
   	if(!boxFinished){//this condition...
   		if(!targetScript2.pushed){//...and this condition need be false to execute the while loop
		while(distance > 0){
			transform.position = Vector3.Lerp(transform.position, boxPuzzlePosition3.transform.position, Time.deltaTime * 0.85);
        	distance = Vector3.Distance(transform.position, boxPuzzlePosition3.transform.position);
        	yield; //yielding to this just means it won't just reach the destination instantly
    	}}
    }
}

OK, first, I can see you are trying to move the current transform towards another object. To do this you are using Lerp.

Lerp is a statement that says, "give me a value that is between X and Y with a time factor of Z (between 0 and 1)

That time factor is your problem. Every loop instance you are mutiplying your current time factor by like 0.02 or something like that.

No matter how much you mutliply your movement, you will never have a distance of zero. thus making it an infinite loop.

instead of Vector3.Lerp, use Vector3.MoveTowards. This gives you a step limit. so if you step by like 0.2 units per second, then you will eventually get there, since there is no multiplication.