Vector3.Distance not working properly?

So I have an object that will change color when the mouse is over it. But only if the player is close enough.

The object's OnMouseEnter function is:

`function OnMouseEnter () { if (Vector3.Distance(player.position,transform.position) <= maxBlockDistance) { renderer.material.color = overColor; } }`

player is the Transform component of.. the player. maxBlockDistance is set to 5.0.

Some items work properly, but others won't have the color even though I am just next to them.

Your function won't ever get called if the mouse is "next to" the object, even if the distance is less than your "maxBlockDist" variable, because you have placed it in the "OnMouseEnter" function, so the mouse has to actually be over the object before the code can run at all. When the mouse is only next to the object, the code isn't even doing that distance check.

If you want to run some code when the mouse is near an object, you'll need to compare the object's position on-screen to the mouse's screen coordinates every frame - something like this:

var proximityRequired = 50
var overColor : Color;
var nearColor : Color;
var withinRange = false;

function Update() {

    var screenPos = camera.WorldToScreenPoint(transform.position);
    var mousePos = Input.mousePosition;
    var proximity = (screenPos-mousePos).magnitude;

    if (proximity < proximityRequired) {
        renderer.material.color = nearColor;
        if (!withinRange) {
            withinRange = true;
            FunctionToCallWhenRangeEntered();
        }
    } else {
        renderer.material.color = normalColor;
        withinRange = true;
    }

}

It might have happened because some or the objects ou attached the script to does not have a collider. Another eeason might be because your collider was not hit because it was too small or cover by some other collider. Attaching this scrit to the player also does not work.

I would like to direct all of you to this question: http://answers.unity3d.com/questions/28547/transform-position-not-changing

It seems Vector3.Distance was not the problem, it was the fact that my first person controller's position never changes, even though I freely move it around. So that's the source problem.

Thanks guys.