Comparing World Positions

So I’ve been trying to find the closest node to a certain object:

	void FindClosestNode (Transform comparitive) {

		float closeDistance = Mathf.Infinity;
		GameObject closestNode = null;
		GameObject[] gNodes = GameObject.FindGameObjectsWithTag("Waypoint");

		foreach(GameObject node in gNodes){
			float dis = (node.transform.position - comparitive.position).sqrMagnitude;
			if(dis < closeDistance){
				closestNode = node;
				closeDistance = dis;
			}
		}

		end = closestNode.transform;
	}

So all looks fine and dandy; I’ve used this code for countless other scripts trying to find the closest… something or other. But, for some reason this code does not work. No matter where I put the comparative, it always chooses this same node. Now I checked the Transforms on each object and I noticed that the comparative was in world space (it’s position was roughly (-12,21,-52)) and for some reason the node (in which the comparative was quite literally standing on) was (-0.0001506805, 0.0002708435, 14.63168) which made absolutely no sense to me since the node was more or less inside the comparative. I realized that the node was a child of another object at (0,0,0). So I figured it must be working in local space. So I tried changing the distance code from:

float dis = (node.transform.position - comparitive.position).sqrMagnitude;

To:

float dis = (node.transform.TransformPoint(node.transform.position) - comparitive.position).sqrMagnitude;

Using transform.TransformPoint to convert it to world space. But the code still doesn’t work. Anything that looks wrong?

Transform.position is always world space. Transform.localPosition is relative to the parent. If you use transform.position you don’t need to use TransformPoint or anything else.