• 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
4

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

Question about animation 2 Answers

How can I attach objects like a picture? 2 Answers

Attach object when NOT in Hierarchy 1 Answer

Hey! how to connect two objects? So to make one? 2 Answers

how to get objects that are in some radius with the point in certain center 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges