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
7
Question by Kenneth Andersen · Mar 29, 2011 at 10:13 AM · gameobjectparentassign

Change parent of GameObject C#

So lets say I got 2 cubes

Cube1 and Cube2

I wan't to assign Cube1 as the Parent of Cube2, can anyone give a code example of how that is done?

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
22
Best Answer

Answer by Jake-L · Mar 29, 2011 at 10:45 AM

Sure,

Cube2.transform.parent=Cube1.transform
Comment
Add comment · Show 7 · 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 techshaman · Jul 31, 2012 at 01:40 PM 0
Share

I tried looping through and adding all my asteroid game objects to a parent game object and they all appeared on top of each other. Not sure why this is happening tho.

avatar image Lenn Dolling · Aug 23, 2013 at 05:56 PM 0
Share

have you tried to set the cube2 transform to a null first then add parent.

avatar image krishnaraj9 · Jun 08, 2015 at 04:19 AM 0
Share

Awesome.. my big problem was solved by your code :)

avatar image McGravity · Oct 08, 2016 at 01:11 PM 0
Share

As this answer can be found on google: There is a SetParent method now. It keeps the local orientation. ScriptReference/Transform.SetParent.

avatar image bitverse · Nov 01, 2017 at 01:58 AM 0
Share

I get the error "CS0103: The name `Cube1' does not exist in the current context"

How do I find Cube1?

avatar image Bunny83 bitverse · Nov 01, 2017 at 02:38 AM 0
Share

Do not revive old, answered questions with comments like that. If you have a question, ask an actual seperate question. If you're new to Unity you may want to read the manual.

Note that while we usually try to do our best to answer any sort of questions around game development with Unity we can't really address basic program$$anonymous$$g questions. If you need more general help you should consider posting your question to the "help room" space of Unity answers.

avatar image thekillerduck07 bitverse · Apr 09, 2020 at 04:43 AM 0
Share

@jordexciu the namespace "Cube1" was a example and is to be replaced with the gameobject of your chosing for ex: Insertyourothergameobjecthere.transform.parent = insertyourgameobjecthere.transform

avatar image
0

Answer by SuryaPrakashModi · Jul 08, 2018 at 03:58 AM

https://www.youtube.com/watch?v=XAiNddXUpH4

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
0

Answer by mrmatt1877 · Dec 30, 2020 at 06:13 PM

parent = gameobject keeps the worldposition of the child while SetParent allows you the choice between world position or changing the position to be relative to the parent.

'cube2.transform.SetParent(cube1.transform, true);

'cube2.transform.SetParent(cube1.transform, false);

'cube2.transform.parent = cube1.transform;

You can see a demonstration of how these will all parent the object but have different results on the position here https://www.monkeykidgc.com/2020/12/tips-and-tricks-unity-set-parent.html

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

8 People are following this question.

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

Related Questions

Can one specify the parent of a gameobject in an array? 2 Answers

Make code effect children of a game object 1 Answer

Is there a way to make a selected object a child of another object during the game? 1 Answer

How to assign a game object with a raycast? 1 Answer

How can I attach a gameobject to another without changing its scale. 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