Unexpected difference between 2 vectors

I have two vectors that look the same, but apperently they are not and I don’t understand why

float distanceToStartPosition = Vector3.Distance(transform.localPosition, startPosition);
Debug.Log(gameObject.name + ": " + transform.localPosition + " <-> " + startPosition + " = " + distanceToStartPosition);
Debug.Log(gameObject.name + ": X: " + transform.localPosition.x + " <-> " + startPosition.x + " = " + Mathf.Abs(transform.localPosition.x - startPosition.x));
Debug.Log(gameObject.name + ": Y: " + transform.localPosition.y + " <-> " + startPosition.y + " = " + Mathf.Abs(transform.localPosition.y - startPosition.y));
Debug.Log(gameObject.name + ": Z: " + transform.localPosition.z + " <-> " + startPosition.z + " = " + Mathf.Abs(transform.localPosition.z - startPosition.z));

Results in

myGameObject: (-2.6, 2.0, 0.0) <-> (-2.6, 2.0, 0.0) = 9.536743E-07
myGameObject: X: -2.6 <-> -2.6 = 0
myGameObject: Y: 2.000001 <-> 2 = 9.536743E-07
myGameObject: Z: 0 <-> 0 = 0

When comparing floats it’s not recommended to use the equality operator.
Unity recommends you use Mathf.Approximately. I know it’s a bit weird but I’ve been using it without problems.

Unity rounds the vector components when displaying (the ToString override), which is probably what you’re dealing with here.

And also Unity overrides the equality operator for vectors so vectors that are nearly equal return true. I wrote a blog post on that recently because it’s unexpected behavior.