Adding Child object in game

Hi,
I am trying to add a child to another child of a hirarchy at runtime. But the parent-child are not following the transform correctly. As in, moving a parent 10 units in x-axis will not move the child the same distance with respect to parent.
The parent has a rigidbody but child are simple mesh objects.
My question is, am i doing it the right way to add child to another child.
code:

public var MountObject : GameObject;
function Awake()
{

  MountObject = Instantiate(MountObject, transform.position, transform.rotation);
  MountObject.transform.parent = transform;
  MountObject.transform.localPosition = transform.localPosition;
}

Thankyou

solved, its right found out after checking all childs that I had changed position in one of the childs update.
:sweat_smile: :sweat_smile: :sweat_smile:

Is this line necessary ?

What is the current result you get ? The child do not follow the parent ? Or by a wrong distance ?

By any chance, do you expect your child to be in the same world position as his parent ? If that’s the case, you don’t need the third line.

Edit : Ok…but is the third line still needed ? :slight_smile:

yeah, third line is not nessary. I was testing for that problem I had.
It was that, I was changing the local position in update somewhere else for another parent that was child to another parent.
It may seem confusing but every child is parent to its child or every parent is child to its parent. think any way and its same.