Why won't my character register as near?

#pragma strict
var player : Transform;
var distance = 7;
private var playerCloseEnough = false;

function Start () {

}

function Update () {
if(Vector3.Distance(transform.position, player.position) < distance){
        playerCloseEnough = true;
    }
    else{
        playerCloseEnough = false;
}
if (playerCloseEnough == true){
		print("It works!");
		}
else
		{
			print("It doesn't work!:(");
			}
}

This is attached to a cube that is supposed to detect when the player is near. So far It doesn’t register as I always get “It doesn’t work!”. Any idea’s?

Your script is written fine and it works. I just tried it out in a new project with a cube and a sphere, where the script was on the cube and the sphere assign as the player. As soon as I moved my sphere inside the range it worked as expected.

I would recommend changing your “It doesn’t work” code to printing the distance so you can see how far away you actually are. Like this:

print(Vector3.Distance(transform.position, player.position));

Why so much ifs? First of all, you could simplify first if as

playerCloseEnough = (Vector3.Distance(transform.position, player.position) < distance

But even that’s not needed, as you can just do:

if ((Vector3.Distance(transform.position, player.position) < distance){
       print("It works!");
       }
else
       {
         print("It doesn't work!:(");
         }
}

This way you’ll avoid unnecessary ifs which may make your game run faster.