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
1
Question by Novaguin · Oct 15, 2011 at 11:50 AM · parentchildrenparentingdroppick up

Parenting 1 object/Removing child/ pick up

Hi there,

I've been thrown into the deep end. My teacher has made our class create a game in Unity after 3 weeks of very basic Javascript "training" (creating basic scripts), hence I know next to nothing about Unity and scripting in the game. I'm trying to create a pick up script using parenting. So far, I have this:

 function OnTriggerEnter (other : Collider) {
 
 transform.parent = Camera.main.transform;
 
 }

It works fairly okay, as in I would probably pass the assignment if I used it. However, I would like to make it better. Is there a way to make it only be able to parent 1 object as I would like to have multiple "children" in the level, but the player can only hold onto 1 at a time. Also, is there a way of removing children after I've parented them. And finally, is there a way to position the object once I've parented it so it appears in the middle of the screen, instead of where ever the player collides into the trigger.

I know I sound like a noob, but that's what I am. Programming is not something I see myself doing in the future however I would like to pass this assignment.

Any help will be greatly appreciated. Thanks in advance.

Comment
Add comment · Show 1
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 pma07pg · Dec 21, 2011 at 10:09 AM 0
Share

I know this isn't an answer but I'm having the same issue. I want the characters of my game to move with a platform if on it but I can't do it with multiple children. It only works with one!

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by aldonaletto · Dec 21, 2011 at 12:46 PM

If all "pickable" objects will use this same script, you can use a static variable to count how many objects are currently in hand, disallowing picking when the max is reached (1 in your case). A static variable is unique: you may have lots of pickable items, but the pickCount variable is the same for all of them:

static var pickCount: int = 0; // global variable that counts currently picked items var maxItems: int = 1; // max items that can be hold var pickPos: Vector3 = Vector3(0, 0.5, 2); // define position relative to player

function OnTriggerEnter (other : Collider) { if (other.tag == "Player"){ // only be picked by the player! if (pickCount < maxItems){ // if player hand not full yet... transform.parent = Camera.main.transform; // pick this object... transform.localPosition = pickPos; // and place it in front of the camera pickCount += 1; // count the item } } }

function DropItem(){ // when releasing the item... transform.parent = null; // set parent to null... pickCount -= 1; // and remember to decrement the pick counter } Notice that I'm comparing tags to check if the object that entered the trigger is actually the player - this disallows other moving things (like lost bullets or enemies) to pick the items. Remember to set the player tag to Player or this script will not work (select the player and click the Tag button in the Inspector, selecting the tag "Player").

Comment
Add comment · 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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to make multiple gameobjects child of one and the same transform (SCRIPT) 2 Answers

Is there a way to limit a parent object's child count to only 1 without individually doing it with each transform? 0 Answers

GetComponent searches an objects child instead of the object 2 Answers

How to check parent value instead name value(more details in post) 3 Answers

Adding child Colliders to your parent object's OnTriggerStay 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