Unity thinks two objects are in same place but they'

Attach the following code to an object, then put the object with the tag “Player” anywhere else as long as the co-ordinates show as different from the position of the object with the script on it.

Now, when you hit “play”, it will tell you the objects are in the same position. Why is this? This happens to me every time.

#pragma strict

var player : GameObject;
var spawner : GameObject;
var player_position_x : float;
var player_position_y : float;

function Start () {

player = GameObject.FindGameObjectWithTag("Player");

}

function Update () {

player_position_x = player.transform.position.x;
player_position_y = player.transform.position.y;
Debug.Log("X: " + player.transform.position.x.ToString() + ", Y: " + player.transform.position.y.ToString());
Debug.Log("X2: " + transform.position.x.ToString() + ", Y2: " + transform.position.y.ToString());

	if(transform.position.x == player_position_x && transform.position.y == player_position_y);
	{
	Debug.Log("SamePosition");
	//player.GetComponent(PlayerScript).collected++;
		for(var more : int; more < player.GetComponent(PlayerScript).collected; more++)
		{
		spawner.GetComponent(RandGeneration).TestObj(gameObject);
		}
		//Destroy(gameObject);
	}

}

even the debug information says that the x and y values are different, so why does it also believe they are the same? I’ve tried this in a scene devoid of any other scripts and get the same results over and over again.

You have a semi-colon at the end of your if. Remove it and you’re good. :wink:

I’ve done that before.

For those who aren’t aware, the semi-colon after the closing parenthesis of the if terminates the line (and therefore the IF statement!). The following bracket is therefore not actually in the if condition. Groups of bracketed code on their own are perfectly legal, so this doesn’t throw a warning or an error, but it is basically never what you want.