What's wrong with my script?

I made this Javascript for picking up and carrying objects.
The goal is for the text crosshair (onto which this script is placed) to show an E when it is hovered over an object with the tag, “thing.” when e is pressed, the object will move to the inhand gameobject in front of the player, and change its tag to held. When e is released, the object should change its tag back to “thing” and fall to ground, physics being enabled again.
For some reason, the object can be picked up and held, but is never released it stays in hand.

Here is the Script:

#pragma strict

var onhand : Transform;

function Start () {
	
}

function Update () {
	 var heldObj = GameObject.FindWithTag("held");
	 
     // Get the ray going through the GUI position
     var ray : Ray = Camera.main.ViewportPointToRay(Vector3(0.5, 0.5, 0));
     // Do a raycast
     var hit : RaycastHit;
     if (Physics.Raycast (ray, hit))
		 if (hit.transform.tag == "thing") 
		 {
	
			 this.GetComponent.<UnityEngine.UI.Text>().text = "E";
			 
			 if (Input.GetKeyDown(KeyCode.E)) {
				hit.transform.tag = "held";
				hit.transform.GetComponent.<Rigidbody>().useGravity = false;
				hit.transform.GetComponent.<Rigidbody>().isKinematic = true;
				hit.transform.position = onhand.position;
				hit.transform.parent = GameObject.Find("FPSController").transform;
				hit.transform.parent = GameObject.Find("FirstPersonCharacter").transform;
			 }
			 
			 else {
				heldObj.transform.tag = "thing";
				heldObj.transform.GetComponent.<Rigidbody>().useGravity = true;
				heldObj.transform.GetComponent.<Rigidbody>().isKinematic = false;
				heldObj.transform.parent = GameObject.Find("House").transform;
				
			 }

		 }

     else
		 this.GetComponent.<UnityEngine.UI.Text>().text = "";
	
 
}

get rid of the else statement for the input and do

	bool selectItem;
	void Update(){
		if (Input.GetKeyDown (KeyCode.E) && !selectItem) {
			// Do your normal logic here.


			// Now change selectItem to true.
			selectItem = true;

		}
		// As soon as we release the key - drop the item IF we have an item selected.
		if (Input.GetKeyUp (KeyCode.E) && selectItem) {
			// Do your logic to drop the item.

			// then change select item to false;
			selectItem = false;
		}
	}

If this helps, please mark as correct so others can know.

Hope this helps!

Well, your raycast is no longer looking at a “thing”, once you pick it up. So the if statement in your raycast will be skipped over, and your GetKeyUp statement will not be registered.

You may want to place your GetKeyUp statement outside of the raycast entirely. Unless you intend for the user to have to look at another “thing” to drop their currently “held”?

Also, don’t use var heldObj = GameObject.FindWithTag(“held”); That is very silly! :wink:
Instead, simply assign heldObj as the ‘hit.transform.gameObject’, in the raycast, when you pick it up.

Lastly, add some open and closing brackets to your if raycast / else statements.

I hope that helps.