Distance calculated inconsistently

Hi guys, I haven’t found a bug like this on the forums so perhaps it’s a new thing or maybe I’m just not identifying the actual problem. Basically I have a simple script to spawn an object at my mouse position when I click the mouse button.

To use an analogy, let’s say I’m spawning an explosion(a sphere) when I left click. I have another script which randomly spawns targets(cubes). Attached to each cube is a script which will look for an instance of an explosion (if there’s no such instance, it’ll keep return and keep looking). If it finds an explosion, it’ll print the distance to that explosion.

My problem is, the distance is being calculated but for some reason the number is inaccurate sometimes. I know this because the visual edge of the explosion is 6 units away from the centre and most of the time a cube that is on the edge of the explosion will display a float representing the distance of around 6 units. However, sometimes it’ll just display a blatantly inaccurate number like 4. Any ideas as to what could be wrong? I have a suspicion the target could be calculating the distance to a previous instance of an explosion but this isn’t supported by my testing.

Here’s a modified version of the script attached to the targets (I’ve tried two ways of calculating the distance but the same problem appears):

var explosion : GameObject;
var distance : float;

function Start () {
	SelfDestruct();
}

function Update () {
	explosion = GameObject.Find("explosion(Clone)");
	if (explosion == null) {
		return;
	}
	//distance = Vector2.Distance(transform.position, explosion.transform.position);
	distance = Mathf.Abs(transform.position.x - explosion.transform.position.x) + Mathf.Abs(transform.position.z - explosion.transform.position.z);
	if (distance > 4.5 && distance < 6.5) {
		transform.renderer.material = null;
	}
	print(distance);	
}

function SelfDestruct() {
	yield WaitForSeconds(2);
	Destroy(gameObject);
}

If you want a distance in the X and Z only, construct two Vector2s and use Vector2.Distance :

var myPos : Vector2 = Vector2( transform.position.x, transform.position.z );
var expPos : Vector2 = Vector2( explosion.transform.position.x, explosion.transform.position.z );

distance = Vector2.Distance( expPos, myPos );