Assign collided object to variable

When the object collides with my player I would like to assign that object to a variable. For some reason my script wont work. They collided but the object is not assigned to current weapon.

var currentWeapon : GameObject;

function OnCollisionEnter(collided : Collision){
if(Input.GetKey(KeyCode.Space) && collided.gameObject.tag == "weapon"){
currentWeapon = collided.gameObject;
}
}

It’s hard to tell without additional information on how your game is set up. Have you tried testing this without the
Input.GetKey(KeyCode.Space)

check?

It may be that the space key is not actually being pressed at the precise moment the collision occurs. Again, this is hard to tell without additional information about the game.