Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Brachiosaurus · Mar 01, 2017 at 07:23 AM · objectstagparent-child

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 = "";
     
  
 }
 
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Brachiosaurus · Mar 02, 2017 at 03:26 PM 0
Share

Thank you so much, It works perfectly now.

avatar image
0

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!

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Brachiosaurus · Mar 01, 2017 at 03:59 PM 0
Share

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! :)

avatar image N1warhead · Mar 02, 2017 at 04:52 AM 0
Share

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;

avatar image Brachiosaurus N1warhead · Mar 02, 2017 at 05:22 AM 0
Share

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 = "";
     
  
 }

avatar image Brachiosaurus · Mar 02, 2017 at 03:27 PM 0
Share

Thank you for helping me, the script wouldn't have worked without the selectItem variable.

avatar image N1warhead Brachiosaurus · Mar 02, 2017 at 10:12 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

,How can I reference all game objects with a tag including clones 1 Answer

Checking trigger collision on other objects? 2 Answers

Changing tagged objects in script 1 Answer

Count the number of objects with a certain tag? 2 Answers

Rotating an object causes it to warp 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges