Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Dax65 · Feb 24, 2014 at 08:33 PM · gameobjectparentloadinventory

How to load an item into the player's hand by clicking on a gameObject.

I need help with my inventory. My game is a third person game with a very simple inventory. For my inventory I want three boxes to be parented with the main camera so they stay on the same spot on the screen. Then when the player clicks on one of the boxes it loads an item into the players hand and when you click it again it gets rid of the item. This is the code I have.

 var Shovel: Rigidbody;
 var Spawn : Transform;
  
 function OnMouseDown() {
 var Shovel1= Instantiate (Shovel, gameObject.FindGameObjectWithTag("SpawnPoint").transform.parent.position, Quaternion.identity);
 Shovel1.transform.parent = this.transform;
 }

It works in that it loads the shovel where the players hand is but once I move the player the shovel stays where it was relative to the camera. But I want it to stay with the hand. Any help would be appreciated.

Comment
Add comment · Show 6
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 highpockets · Feb 24, 2014 at 10:00 PM 0
Share

What is this script attached to? If it is attached to the camera the parent will be the camera, if it is attached to the hand the parent will be the hand.

avatar image highpockets · Feb 24, 2014 at 10:05 PM 0
Share

If this script is on the camera and you want it to stay on the camera. Then do this:

Shovel1.transform.parent = GameObject.Find("HandObjectNameInHeirarchy");

avatar image Dax65 · Feb 25, 2014 at 12:35 PM 0
Share

When I use that code I get this error.

Assets/Code/InventoryChanger.js(14,59): BCE0022: Cannot convert 'UnityEngine.GameObject' to 'UnityEngine.Transform'.

avatar image CodeElemental · Feb 25, 2014 at 01:09 PM 0
Share

You need to actually reference the transform.

 Shovel1.transform.parent = GameObject.Find("HandObjectNameInHeirarchy").transform;
avatar image Dax65 · Feb 25, 2014 at 01:24 PM 0
Share

Thank You!!! It works now. How do I make your comment the answer?

Show more comments

1 Reply

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

Answer by Dax65 · Feb 25, 2014 at 04:47 PM

Thank you CodinRonin for your answer. It now works properly with this code for those who will have the same problem in the future.

 var Shovel : Rigidbody;
 var Spawn : Transform;
  
 function OnMouseDown() {
 var Shovel1= Instantiate (Shovel, gameObject.FindGameObjectWithTag("SpawnPoint").transform.parent.position, Quaternion.identity);
 Shovel1.transform.parent = GameObject.Find("SpawnPoint").transform;
 }
Comment
Add comment · Show 3 · 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 CodeElemental · Feb 26, 2014 at 08:00 AM -1
Share

It's not ok to rewrite what people answer you youreself and mark it as you answered the question. I downvoted.

avatar image Dax65 · Feb 26, 2014 at 06:47 PM 0
Share

That is what I had to do because your answer was a comment not an answer. I did this so people can know what I did that fixed it to help people in the future who have the same problem.

avatar image Dax65 · Feb 26, 2014 at 06:48 PM 0
Share

And I gave you credit at the top.

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

21 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

Related Questions

Spawn inside of a parent 1 Answer

"Center On Children" programmatically 1 Answer

Trying to make a simple inventory: 0 Answers

Objects are destroyed/disfunctional after changing scene 3 Answers

Hard question about saving and loading 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