picking up objects - not work correctly

I asked a question yesterday about picking up some boxes someone kindly helped me and the code i have looks like this:

var holdingAnItem : boolean;
var itemCurrentlyHeld : Transform;
var headCamera : Transform;
var pickupRange : float;
var carryOffset : Vector3; // position of a carried object relative to the camera

function Update () {
    if (holdingAnItem && itemCurrentlyHeld) {
        itemCurrentlyHeld.position = headCamera.position + carryOffset;
        itemCurrentlyHeld.rotation = headCamera.rotation;
    }
}


function Pickup () {
    if (holdingAnItem) { return; } // stop if we're already holding something
    var hit : RaycastHit;
    if (Physics.Raycast(headCamera.position, headCamera.forward, hit, pickupRange)) {
        if (hit.collider.CompareTag("carryable")) {
            itemCurrentlyHeld = hit.collider.transform;
            holdingAnItem = true;
        }
    }
}

function Drop () {
    if (!holdingAnItem) { return; } // stop if we're not holding anything
    itemCurrentlyHeld = transform;
    holdingAnItem = false;
}

ive tagged the boxes and applied this code to the play/character. if i walk up to the box nothing happens, however if i check the box next to holding and item in the inspector it works how i want.

why is it not picking up the box as i walk up to it?
i would like it to be automatic or have it so i press a button to pick up the box.

hope someone can help,

many thanks matt

It doesn’t perform any pickup actions at all. Looking at your old question and the answer you got, it is just psuedo code. It means it isn’t tested, it’s a general draft of how to do it.

var carrying : Transform;

function Update () {
    if (Input.GetButtonDown("Fire"))
        if (carrying) Drop();
        else          Grab();
}

function Drop() {
    carrying.parent = null;
    carrying = null;
}

function Grab() {
    var hit : RaycastHit;
    if (Physics.Raycast(transform.position, transform.forward, out hit)) {
        carrying = hit.collider.transform;
        carrying.parent = transform;
    } 
}

This code is also untested, but it should work. The script is supposed to go on the camera, and will grab any object with a collider and set it as child to the camera (so it follows it around). If you pick up any object with a dynamic rigid body on it, then you’re up for trouble as it attempts to simulate while still being carried. In that case you have to modify the code to set it being kinematic.