• 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 /
  • Help Room /
avatar image
0
Question by sid4 · Oct 06, 2016 at 01:15 PM · script.

PIckup is not working . I cant pick up my objects to throw.please help

I cant pick up the objects I have tagged pickup. I created the empty gameobject "holdposition" and tried attaching script to that but sill nothing. this is the script I have below I attached script to the player to pick up the objects but I cant pick them up with pressing the space bar do I attach the script to the objects? I tried that to but its still not working using UnityEngine; using System.Collections;

  public class ThrowTest : MonoBehaviour
 5.  {
      public Transform HoldPosition; //This is the point where the "hands" of the player would be
      public float ThrowForce = 10f; //How strong the throw is. This assumes the picked object has a rigidbody component attached
      public float AlphaAmount = 0.5f; //this will be the alpha amount. It's public so you can change it in the editor
  
 10.          private Transform _pickedObject;
      private Color _originalColor;
  
      void OnCollisionEnter (Collision collision)
      {
 15.                 if ( collision.transform.tag == "pickup" )
          {
              //caches the picked object
              _pickedObject = collision.transform;
  
 20.                          //caches the picked object's color for resetting later
              _originalColor = _pickedObject.GetComponent<Renderer>().material.color;
  
              //this will snap the picked object to the "hands" of the player
              _pickedObject.position = HoldPosition.position;
 25.              
              //this will set the HoldPosition as the parent of the pickup so it will stay there
              _pickedObject.parent = HoldPosition;
  
              //this will change the alpha amount on the object's color to make it half transparent
 30. _pickedObject.GetComponent<Renderer>().material.color = new Color(_pickedObject.GetComponent<Renderer>().material.color.r,
                  _pickedObject.GetComponent<Renderer>().material.color.g,
                  _pickedObject.GetComponent<Renderer>().material.color.b,
                  AlphaAmount);
          }
 35.              }
  
      void Update()
      {
          if (Input.GetKeyDown("space"))
 40.                      {
              if (_pickedObject != null)
              {
                  //resets the pickup's parent to null so it won't keep following the player
                  _pickedObject.parent = null;
 45.                  
                  //resets the pickup's color so it won't stay half transparent forever
                  _pickedObject.GetComponent<Renderer>().material.color = _originalColor;
  
                  //applies force to the rigidbody to create a throw
 50.                              _pickedObject.GetComponent<Rigidbody>().AddRelativeForce(transform.forward * ThrowForce, ForceMode.Impulse);
  
                  //resets the _pickedObject 
                  _pickedObject = null;
              }
 55.                      }
      }
  }
 
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

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

75 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

warning CS0618 with unity ngui 0 Answers

Same Q as before but it has been a while and nones replyed so im reposting 2 Answers

How do i get an object to destroy another after multiple hits? 0 Answers

How to set float, bool from one script to the other? 1 Answer

level generation 1 Answer


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