- Home /
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?
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220612212336im_/https://answers.unity.com/themes/thub/images/avi.jpg)