• 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
1
Question by Jordan Miller 2 · May 05, 2010 at 03:27 AM · movementparenthierarchy

GameObject changes position when made a child.

in my game there is a unit who at some point is made a child under another object. when this happens my child object aligns its self with the parent object which is strange to me. I don't want it to move when it becomes a child, can you help me? here's that part of my code:

    moo = Instantiate(genmover, midpoint, Quaternion.identity);//genmover is a character controller, and midpoint is just a point I have defined above.
    moo.transform.name= "mover" + hutu;//hutu is a string, no problem here.
    moo.transform.parent = gameObject.Find(typeis[i]+"bots").transform; //typeis[i]+"bots" adds up to some gameobject name that in the game and its the parent of moo. but 
//moo becomes a parent here in a sec, and it's child is the one that moves.  
    gameObject.Find("Girlbots/"+numberstring).transform.parent = moo.transform;
//this is the part where my gameobject becomes a child under moo.
//it is at this point that the gameObject relocates. why? and how do I get it to stop?
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by duck · May 05, 2010 at 10:01 AM

Changing the parent of a transform does not affect the transform's world position or rotation. The local position and rotation of the child is automatically modified so that the object remains in the same world-relative space and orientation.

I have many scripts which rely on this, and in fact I also have some scripts where I want to preserve the object's local transform instead, and it's in these cases where I must correct the values localPosition and localRotation after parenting - not the other way around. This behaviour is also documented.

This means there must be a problem somewhere else in your code - perhaps there's a script attached to your "genmover" prefab which moves it in its "Start" function? If so, that script would execute after all the current Update scripts of the current frame have finished which would perhaps make it seem as though it's moving as a result of the script you're looking at.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Why are my GameObjects altering themselves? 0 Answers

Disastrous clutter in hierarchy panel due to feature 3 Answers

Collider working but not firing OnCollisionXYZ (in parent object) 0 Answers

getting the object that the running script is attached to 1 Answer

find next(not necessarily immediate next) child of another parent in hierarchy with tag "checkpoint"? 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