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 erisee · Jun 29, 2012 at 02:12 AM · javascriptparentchildchildren

Update Parent/ Children From Script?

Is it possible for me to make an object a child of another object when a certain button is pressed, and vice versa?

I know how to use input, just need to know how to add/ subtract children from script.

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

Answer by always_beta · Jun 29, 2012 at 02:15 AM

Do you mean: childObject.transform.parent = parentObject.tranform

Comment
Add comment · Show 6 · 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 erisee · Jun 29, 2012 at 02:20 AM 0
Share

I guess? Let's say I have a objOne and objTwo. How do I use that to make objTwo a child of objOne?

avatar image erisee · Jun 29, 2012 at 02:35 AM 0
Share

Okay, after reading the script reference, I think I understand. So now I have this:

function OnCollisionEnter(theCollision : Collision){ if(theCollision.gameObject.name == "item"){ if(Input.GetButtonDown("Pick Up")){ item.parent = transform;

But it doesn't recognize the "item" in item.parent in the last line. It says "$$anonymous$$ identifier: 'item'"

avatar image always_beta · Jun 29, 2012 at 02:35 AM 0
Share

So assume both objOne and objTwo are instances of GameObject, then: objTwo.transform.parent = objOne.transform.

avatar image always_beta · Jun 29, 2012 at 02:38 AM 0
Share

You should use theCollision.gameObject.transform.parent = transform ins$$anonymous$$d of item.parent.

avatar image erisee · Jun 29, 2012 at 03:15 AM 0
Share

I don't under stand what you mean when you say "are instances of GameObject"..

Show more comments
avatar image
0

Answer by aldonaletto · Jun 29, 2012 at 04:30 AM

@always_beta means that objOne and objTwo are GameObject references. Contrary to Flash and web javascript, in Unity the object name cannot be used as a reference to the object - Unity uses reference variables, which are the equivalents of C pointers - a reference holds the object address in memory (kind of... things are somewhat different in .Net/Mono).
In the case above, theCollision gives you several references to the object hit (gameObject, transform), which you can use to set parenthood:

function OnCollisionEnter(theCollision : Collision){
  if (theCollision.gameObject.name == "item"){
    if (Input.GetButtonDown("Pick Up")){
      // Collision has a reference to the object's Transform as well
      // You must use it, since parent is a Transform property:
      theCollision.transform.parent = transform;
    }
  }
}
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

6 People are following this question.

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

Related Questions

Create multiple instances of an object 2 Answers

Make a simple tree 1 Answer

GetComponentInChildren(Renderer).active wont work? 2 Answers

how to put the objects together (javascript) 1 Answer

Access parents other child from other other child 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