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
4
Question by cworboys · Apr 15, 2011 at 04:25 AM · objectsattach

How Do I Programatically Attach Objects To Each Other?

Hello. I am trying to programatically attach objects to each other so they behave as one object when attached. Forgive me if this question has already been asked but I couldn't find a suitable answer on here to help me out. If I missed it please kindly point me in the right direction. I am very new to programming and am using C# so example code would be appreciated.

Ok so here's what I'm trying to do...

I have a bunch of blocks (cubes) that I would like to randomly attach to each other in code to form various shapes (like Tetris pieces). When attached, these newly formed shapes should act as a single object and be able to be dragged around as a solid shape without breaking apart. They should still visually appear as composed of the initial cubes that made them up (just stuck together in various shapes).

I don't know if that is clear enough but any help would be greatly appreciated. Thank you.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
9

Answer by DaveA · Apr 15, 2011 at 05:31 AM

Set transform.parent = otherObject.transform to attach this object to the other object for example.

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
avatar image
5

Answer by SpacePilot1000 · Apr 05, 2014 at 04:55 AM

When attaching objects, I also found it useful to disable collisions on the object being attached. For example:

 // Disable collisions with the object being attached
 if(Object1.collider2D)
 {
     Object1.collider2D.enabled = false;
 }
 
 // Don't allow physics to affect the object
 if(Object1.rigidbody2D)
 {
     Object1.rigidbody2D.isKinematic = true;
 }
 
 // Attach object 1 to object 2
 Object1.transform.parent = Object2.transform;
 
 // Center object 1 on object 2 (no offset)
 Object1.transform.localPosition = Vector3.zero;
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 voOID · Jun 05, 2016 at 10:21 PM 0
Share

Thanks, this helped my attached objects to stay still when i applied force to the parent.

avatar image
1

Answer by mark.gossage · May 22, 2012 at 02:14 AM

Just make sure that you watch the relative positions & rotations.

I have:

 GameObject objA=...;
 GameObject objB=...;
 // attach a to b
 objA.transform.parent=objB.transform;
 // make sure its exactly on it
 objA.transform.localPosition=Vector3.zero;
 objA.transform.localRotation=Quaternion.identity;

And its fine.

Comment
Add comment · Show 2 · 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 Ryoshi217 · Aug 15, 2012 at 06:51 AM 0
Share

it doesn't work for me any suggestions?

avatar image mark.gossage · Aug 15, 2012 at 11:02 AM 0
Share

Be more specific than 'its broken' Explain whats the matter & I might be able to advise.

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

Attach object when NOT in Hierarchy 1 Answer

Question about animation 2 Answers

How can I attach objects like a picture? 2 Answers

Calling 2 objects into script for onCollisionEnter? 1 Answer

Understanding Add component and classes. 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