Chad.H
Chad.H

Reputation: 3

Multiple Update Scripts, not working with Multiple Objects

Currently I have 8 objects, each with their own Update Script. What I'm trying to do, is have each object show that it is in the right location with bools. If it leaves the right location, the bool returns to false.

For the first two objects, the script works great. The first script is

p1, with x = -2.5, and y = -2.5.

2nd Object is

p2, with x = 0, and y = -2.5.

When they leave those positions, the bool changes to false without any issues.

Then when I get to the 3rd object,

p3, with x=-2.5 and y=0

But it shows true for -2.5 x, -2.5 y, when it should be false. Any help would be greatly appreciated.

void Update(){
    if(transform.position.x <= -2.5 && transform.position.y <= -2.5) {
    p1 = true;
    } else {
      p1 = false;
      }
}

Upvotes: 0

Views: 66

Answers (1)

Frank J
Frank J

Reputation: 1696

Look at the condition you wrote: you are testing for <= -2.5. By definition -2.5 is less or equal than -2.5 (equal in this case) therefore it will fall into the true case. if you want -2.5 be false change it to < (less than) instead of <= (less or equal).

Update:

From you comment I suspect the your test on p3 is for x <= -2.5 and y <= 0 If y changes from 0 to -2.5 you second condition is still true because -2.5 <= 0. If you want to test for just one correct spot you have to test for != (unequal) that number, which however might cause other problems depending on the datatype you are using. E.g. For double you might have to test Math.Abs(-2.5 - y) < 2d * Double.Epsilon.

For more info on that particular look e.g. here and here.

Upvotes: 1

Related Questions