# Comparing two equal vectors does not produce true

Hi,

So I have an issue with this particular function

``````    GameObject GetFutureCube(GameObject[] Cube, Vector3 futurePosition, GameObject NULL) {
GameObject obj = NULL;
foreach (GameObject element in Cube) {
Debug.Log(element.name + " " + element.transform.position + " " + futurePosition);
if (element.transform.position == futurePosition) {
obj = element;
break;
}
}
return obj;
}
``````

So what it does is just take variale futurePosition and compare it to the positions of objects Cube in the scene.

And it does not work all the time, thats why I put

``````Debug.Log(element.name + " " + element.transform.position + " " + futurePosition);
``````

to debug to see whats going on there. And on one of the iterations it prints out:

``````Cube (7) (1.0, -1.0, 0.0) (1.0, -1.0, 0.0)
``````

Which means that the function should return Cube(7), but instead it returns NULL

Comparing floats is already a bit of a gamble due to floating point inaccuracy. A vector3 contains three of them so you’d make the odds even worse.

You should use the magnitude:

``````Vector3 vec = element.transform.position - futurePosition;
float mag = vec.magnitude;
if(mag < 0.1f){ // Close enough }
``````

Which is the same as:

``````Vector3 pos =element.transform.position;
float xDiff =  Mathf.Abs(pos.x, futurePos.x);
float yDiff =  Mathf.Abs(pos.y, futurePos.y);
float zDiff =  Mathf.Abs(pos.z, futurePos.z);

if(xDiff < 0.1f && yDiff < 0.1f && zDiff < 0.1f){ // Close enough }
``````

if the game is 2D, you know that z is irrelevant and you can skip it. You can also use Mathf.Approximately

Actually I managed to fix it some other way. But It might have been the floating point inaccuracy issue anyway.

The issue was in

``````Vector3 futurePosition
``````

which was derived from a class that looked for an objects Player position, moved by coroutine. Once I rounded the Player position inside coroutine

``````float x = Mathf.Round(Player.transform.position.x);
float y = Mathf.Round(Player.transform.position.y);
float z = Mathf.Round(Player.transform.position.z);
Player.transform.position = new Vector3(x, y, z);
``````

everything started working fine.

Try this

``````element.transform.position.Equals(futurePosition)
``````