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 Locomalito · Nov 26, 2014 at 12:55 PM · transform.positionparent transform

Cannot position child object relative to parent.

I created a parent game object at run time. Set it's position. Then, added another gameobject to it. I want the child game object to be @ (0,0) of the parent. Did some research but still can't make it work. Here is my code:

 GameObject myParentObject = new GameObject();
 myParentObject.transform.position = new Vector2 (-4.18f, -1.83f);

 GameObject myChildObject = new GameObject();
 myChildObject .transform.parent = myParentObject.transform;

A weird thing is I see the myChildObject nested as a child of the myParentObject, I click on it and it shows in the inspector the x=4.18 and y=1.83. Yes, they became positive values. Not sure why. All I want to do is create a parent object then create my first object @ (0,0) of that parent object by default. That's how flash/as3 handles things like these. I'm a bit confused.

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

Answer by Baste · Nov 26, 2014 at 01:03 PM

The position shown in the inspector of a child object is the child object's offset from the parent.

So if your parent object is at -4.18/-1.83, and the child object is at 0/0, the child object will show up in the inspector with the coordinates 4.18/1.83.

Setting a transform as a child of another transform does not move the child. It just ensures that the child moves and rotates with the parent. To make your child move to exactly the same position as the parent, you have to actually move it:

 GameObject myChildObject = new GameObject();
 myChildObject.transform.parent = myParentObject.transform;
 myChildObject.transform.position = myParentObject.transform.position;

You can also use the localPosition value, which is the relative position with regards to the parent object:

 GameObject myChildObject = new GameObject();
 myChildObject.transform.parent = myParentObject.transform;
 myChildObject.transform.localPosition = new Vector2(0,0);

Hope that helps!

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 Locomalito · Nov 27, 2014 at 03:45 AM 0
Share

It worked! Nicely explained!

avatar image russisunni · Sep 27, 2020 at 11:46 AM 0
Share

it worked. Thanks!

avatar image
6

Answer by smoi · Nov 26, 2014 at 01:22 PM

when you parent a child to a gameobject, it will keep the absolute postion by appling the inverse transform.

after parenting you probably want set: myChildObject.transform.localPosition = Vector3.zero;

Comment
Add comment · Show 3 · 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 Locomalito · Nov 27, 2014 at 03:46 AM 0
Share

this worked too! thanks!

avatar image ultimatearcade · Aug 11, 2016 at 03:32 AM 0
Share

Thank you so much for explaining this :)

avatar image halivudestevez · Feb 03, 2018 at 03:46 PM 0
Share

At some part, my child object is moved wrongly.

I had to put this Vector.zero to the Update method.

avatar image
0

Answer by popugames · Aug 17, 2018 at 08:37 PM

I found the best solution
just put this in your child script

 private void Update()
     {
         if(transform.parent.position != transform.position)
         transform.parent.position = transform.position;
         transform.localPosition = Vector3.zero;
     }

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

Position of cameras children objects messed up after the game start. 0 Answers

transform.position vs interface position 1 Answer

Game Object Teleport's to to players starting position. 3 Answers

How to change the speed of "transform.position"? 2 Answers

Issue finding an angle with trigonometry 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