SORPG: Distance problem?

So im making a SORPG (singpleplayer offline role playing game) and im currently working on a harvest tree type of resource gathering.

What i m trying to do is when you are within 8 "distance" from the tree you can bring up the menu by left clicking (right click is reserved for my camera script)

upon clicking ( and i know that he, meaning my player1, is close enough to the tree the menu does not appear)


//Menu Settings
var MenuActive1:boolean = false; 
var timer : float = 5;

//Find closest Tree/Click on tree?
function Update () {
//Finding Closest Tree
    var waypoints: GameObject[] = GameObject.FindGameObjectsWithTag("ClickAble"); 
    var closest: GameObject; 
    var closestDist = Mathf.Infinity; 

    for (waypoint in waypoints) { 
        var dist = (transform.position - waypoint.transform.position).sqrMagnitude;         
            if (dist < closestDist) { 
                closestDist = dist; 
                closest = waypoint; 

    //LookAt Closest (not using)
    //Click Code
    if (Input.GetMouseButton(0)) {
        var ray: Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        var hit: RaycastHit;
        var wantedPosition= Vector3(hit.point.x, hit.point.y, hit.point.z);
        var playerDist =    (transform.position - wantedPosition).magnitude;
            Debug.Log( playerDist ); // (display distance from nearest "clickable" object)
        if(playerDist <= 10 && timer == 5 ){        
        timer = 0;
            if (Physics.Raycast(ray, hit)) {                
                //clickable object names
                switch ( ){
                    case "HarvestTree": //(going to add more case "object name here as progress")               
                            MenuActive1 = !MenuActive1;             //(not showing up!!!)           
        else {timer += 1;}          

function OnGUI () { 

    if (MenuActive1 == true) 
       if (MenuActive1 == true){ 
         // Make a background box 
       GUI.Box (Rect ((Screen.width-31)/2,190,220,180), "Large Tree


            //Second button returns to main menu 
            if (GUI.Button (Rect((Screen.width-31+200)/2,285,80,20), "Accept")) { 

            //Second button returns to main menu 
            if (GUI.Button (Rect((Screen.width-31+200)/2,320,80,20), "Cancel")) { 
            //Closes the menu

The `var playerDist = (transform.position - wantedPosition).magnitude;` is calculating the distance between where the ray hits and the player... but for some reason does not activate the if statement below it. the timer is always at 5 unless you click something so thats not the problem.

Thanks for the help, Raul

PS. don't point out horrible structure im a newbie.

You're testing against nothing with your magnitude, as wanted position hasn't been setup properly. You're using the values from hit, however you haven't performed the Raycast at that point, so hit doesn't actually contain any data yet. I believe by default that means you're actually always setting wanted position to 0,0,0 which will always fail your distance test.