# How to synchronize a parent and child position?

When watching this video of Unity development, I was following along trying to learn Unity and for the most part understood everything conceptually.

Where I got lost was when I tried using some of the same code and setup in Unity. I got the code working fine, but I got in trouble when I tried to implement the parent-child behavior seen in the video at around 7 minutes regarding the Enemy objects.

The solution described is set up as follows (someone correct me if I didn't understand it properly from the video).

Parent Object

• Contains the script which drives behavior and movement
• Is located at the origin of the scene (reset the transform in other words)

Child Object

• Contains the graphics renderer which draws the actual sprite or 3D object
• Is located at the enemy spawn point

For me to get this setup working, I had to run the Translate function explicitly against the child object:

transform.GetChild(0).Translate(dir.normalized * distThisTurn)


However, in the video, it appears to work just fine with only:

transform.Translate(dir.normalized * distThisTurn)


Once he fixes the rotation issue, at about 7 minutes in, this works beautifully, and the enemy travels the path exactly as expected. When I tried this exact same approach, my enemy sprite jumps all over the place. It took me several hours to figure out that I needed to add the .GetChild(0) part. The behavior I saw with the setup from the video is that the invisible parent object moved correctly along the path, and the child went crazy and followed some alternate version of the same path, as if the origin was different or offset, or the path was reflected across some axis.

My question is: what did I do wrong? Can anyone spot how my setup is different than the one used in the video? Did I miss some detail of how he set up his parent-child objects, particularly for the enemies? Or could this be some different behavior due to the version of Unity he is using (I believe mine is newer as I installed it just this week). I think it could also be some small gizmo or feature he neglects to mention that allows you to synchronize the movement of a parent object with its children.

The reason for this setup (at least as given in the video) is it allows you to easily swap out the graphics. The scripting is tied to the parent, so all you have to do is exchange the child for one with different graphics and you're done. That part makes sense, I just don't understand why the exact same code he uses did not work for me. I'm talking only about the Enemy object here - the creeps that move along the path (it's a tower defense game).

I just want to know because I may have to use this extra GetChild call quite a bit, and I feel like it shouldn't be necessary, especially after seeing it work so well in the video. Doing this would also make my scene design a little easier.

I have already read the comments on that video for additional info, as well as looked through Unity forum posts, other posts here on Game Dev.SE, and Google results. It would seem as if this is either old behavior, or there is some small detail neglected to explain in the video of how to synchronize movement of parent and child objects.

• Your description of the parent-child relationship does not appear to match the video. In the video, the parent object is not located at the scene origin. The script moves the parent object, which represents the enemy as a whole (while the child object just represents the enemy's visuals - manipulating the child directly breaks the intended separation of the visuals from the enemy's behaviour). You can see this around 16 minutes in when the enemy is moving - the parent object is selected and you can see it's the parent transform that is changing over time. – DMGregory Dec 30 '16 at 3:33
• Thanks, I see you're right about that and I understand what you're saying. I guess that means I have a different question then: how do I get the parent object to use the child object's visuals? I did originally have the parent moving correctly, but it was invisible and did not use the visuals from the child. – Tim Dec 30 '16 at 16:02
• The parent has no visuals at all. When you move the parent, the child object moves with it. So the visuals you see are the child's alone, and are not "used by" the parent. It's possible that you inadvertently hid the child object, positioned it far from the parent's origin so that it was off-screen once the parent started moving, or a number of other cases. Consider showing us the inspector & hierarchy for your enemy objects to help diagnose the issue. – DMGregory Dec 30 '16 at 16:05