# Vector3.Distance is returning a whacked out value

So this is the relevant code.

``````public void FormRoof ()
{
for (x = Pos.x; x < Pos.x+r; x++)
{
for (y = Pos.y; y < Pos.y+r; y++)
{
for (z = Pos.z; z < Pos.z+r; z++)
{
Instantiate(Cube, new Vector3(x,y,z), Quaternion.identity);
Cube.tag = ("Builder");
}
}
}
Instantiate(Point, new Vector3(x-halfr, y-halfr, z-halfr), Quaternion.identity);
}

public void FormRoof2 ()
{
Cubes = GameObject.FindGameObjectsWithTag ("Builder");
foreach (GameObject Cube2 in Cubes)
{
float dis = Vector3.Distance(Cube2.transform.position, Point.transform.position);
if (dis < halfr)
{
Destroy(Cube2);
}
Debug.Log("dis" + dis);
}
}
``````

But the value that Vector3.Distance is returning is between 55 and 75 even for cubes that are right next to the Point object.

Ahh, you’re assigning a tag to the PREFAB and NOT the instantiated gameObject. Furthermore, the tag should be assigned to the instantiated gameobject.

Something like this…

``````  GameObject go = (GameObject) Instantiate(Cube, new Vector3(x,y,z), Quaternion.identity);
go.tag = ("Builder");
``````

and you must HOLD the reference of the instantiated point. I’m not sure why you’re instantiating it at run time but here it goes. Assigning a name to it will lead a better way to find it in code later on, but still there are many options too.

``````  GameObject myPoint= (GameObject) Instantiate(Point, new Vector3(x-halfr, y-halfr, z-halfr), Quaternion.identity);
myPoint.name = "Point";
``````

and in your FormRoof2() function, add this line below the cubes array variable…

`````` GameObject testPoint= GameObject.Find("Point");
``````

Also, make sure to replace the parameter of the INSTANTIATED point with the one you’re referencing from the prefab.

`````` float dis = Vector3.Distance(Cube2.transform.position, testPoint.transform.position);
``````

Hope this helps you out.