- Home /
Question by
The_Toller · Oct 31, 2015 at 10:32 AM ·
c#if statement
Why is this if-statement returning true?
Hello! I have a really weird issue.
The following code is always printing the Debug.Log code. Even when distance is greater than range. Why is that if-statement always returning true? What am I missing? The Debug.Log always prints the distance and the range. If it is "1/10" that is correct since its below range. But it also prints "3242/10" if i stretch the objects apart. It's never false.
Thanks in advance for any help!
private IEnumerator DoActions()
{
float distance;
while(target)
{
distance = Vector3.Distance(transform.position, target.transform.position);
if(distance < range);
{
Debug.Log (distance + "/" + range);
//Do things
}
yield return null;
}
}
Comment
Best Answer
Answer by Bunny83 · Oct 31, 2015 at 10:44 AM
Here:
if(distance < range); <-----
you have a semicolon after your if. That will terminate it at the same line. So the following block doesn't belong to the if statement at all. Just remove that semicolon ^^.
Omg. Lol. Haha! I was certain it was some really nooby mistake. Thanks a lot! :D