# Function to cheack if there is already a gameobject in a indicated vector not working

Hello i have the following function that will retrieve a true of false statement depending if there is already a gameobject with a tag of tiles in a de-terminated location

``````    bool CheackGameObjectPos(Vector3 Pos)
{
GameObject[] Array = GameObject.FindGameObjectsWithTag("Tiles");
foreach (GameObject Game in Array)
{
Vector3 ZeroPos = new Vector3(0, 0);
if (Pos.x == ZeroPos.x && Pos.y == ZeroPos.y)
{
return true;
}
Vector3 NewPos = new Vector3(Pos.x, Pos.y, 0);
if (Game.transform.position.x == NewPos.x && Game.transform.position.y == NewPos.y)
{
return true;
}

}
return false;
}
``````

For some reason that i cant figure out it sometimes does not work

Please note that a position contains float values, so the position needs to be exact.
If you pass in a vector that has x on 0.34 and you are comparing to one that has position 0.35 then it will return false.

I also simplified it a little into LINQ statements.

``````        bool CheckGameObjectPosition(Vector3 position)
{
//Check the zero position first
var zeroPosition = Vector3.zero;
if (position.x == zeroPosition.x && position.y == zeroPosition.y)
{
return true;
}

//Get all tile objects and select their position
var positions = GameObject.FindGameObjectsWithTag("Tiles")
.Select(f => f.transform.position);

//If there is any of the positions equal to our given position
if (positions.Any(f => f.x == position.x && f.y == position.y))
{
return true;
}

//Else we return false
return false;
}
``````

The problem was when a float point imprecision happen the code would say broths vectors where different even that the difference as just 0.0000001 so i add the follow code

``````            if (Mathf.Approximately(Game.transform.position.x, NewPos.x) && Mathf.Approximately(Game.transform.position.y, NewPos.y))
{
return true;
}
``````

This Codes uses a Mathf function that compares 2 floats and return if they are similar if yes it will return true saying that a vector already exists
So basically this is what the code looks like

``````bool CheackGameObjectPos(Vector3 Pos)
{
GameObject[] Array = GameObject.FindGameObjectsWithTag("Tiles");
foreach (GameObject Game in Array)
{
Vector3 ZeroPos = Vector3.zero;
if (Pos.x == ZeroPos.x && Pos.y == ZeroPos.y)
{
return true;
}
Vector3 NewPos = new Vector3(Pos.x, Pos.y, 0);
if (Mathf.Approximately(Game.transform.position.x, NewPos.x) && Mathf.Approximately(Game.transform.position.y, NewPos.y))
{
return true;
}
}
return false;
}
``````

@ShadyProductions thank you a lot for your answer but the code was a little bit difficult to understand