- Home /
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 = "";
}
Answer by Lylek · Mar 02, 2017 at 06:33 AM
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! ;) 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.
Answer by N1warhead · Mar 01, 2017 at 09:16 AM
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!
I did as you said, N1warhead, and the boolean successfully toggles to true when the object is picked up, but still, for some reason, I can't drop it. Could there be something wrong with my logic for dropping it? Also, thank you for replying so soon! :)
Sorry I hadn't even realized you were using UnityScript I hoped that didn't cause much difficulty for you trying to figure out how to translate it. I don't remember Unity Script much, but the only other thing I can think of is to make the "heldObj" null so the raycast don't hold it any longer.
Which in doing so might cause the raycast go crazy with errors so you have to account for null values.
So in C# (sorry as mentioned don't remember much of U Script) you can do something along the lines of
if(heldObj != null){ // Do Raycast. }else{ // Ignore Raycast. }
So right above the selectItem = false just make the heldObj null by doing heldObj = null;
Hello again, and thanks again for the response, I set the heldObj to null, and there was no effect, so I began to wonder if the Script could even tell if I was pressing e while the object is held. I made it change the text when e was pressed so I could tell that it received it, but when I tested it out, the text did not change. So I think my problem has something to do with the input (keypress) not being received. By the way, since it has changed a bit, here is the code:
#pragma strict
var onhand : Transform;
var selectItem : boolean;
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.Get$$anonymous$$eyDown($$anonymous$$eyCode.E) && selectItem == false) {
hit.transform.tag = "held";
hit.transform.GetComponent.<Rigidbody>().useGravity = false;
hit.transform.GetComponent.<Rigidbody>().is$$anonymous$$inematic = true;
hit.transform.position = onhand.position;
hit.transform.parent = GameObject.Find("FPSController").transform;
hit.transform.parent = GameObject.Find("FirstPersonCharacter").transform;
selectItem = true;
}
if (Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.E) && selectItem == true) {
this.GetComponent.<UnityEngine.UI.Text>().text = "I";
heldObj.transform.GetComponent.<Rigidbody>().useGravity = true;
heldObj.transform.GetComponent.<Rigidbody>().is$$anonymous$$inematic = false;
heldObj.transform.parent = null;
heldObj.transform.tag = "thing";
heldObj = null;
selectItem = false;
}
}
else
this.GetComponent.<UnityEngine.UI.Text>().text = "";
}
Thank you for helping me, the script wouldn't have worked without the selectItem variable.
You're welcome man, I see how the other guy saw what was wrong, not the best with Unity Script (I ain't). So I totally missed how you were even grabbing the object lol.
But I'm glad you got it working and I'm glad I was able to give something of help.
Take care, $$anonymous$$.
Your answer
![](https://koobas.hobune.stream/wayback/20220612112211im_/https://answers.unity.com/themes/thub/images/avi.jpg)