'How to move child object to parent's location?

How would I go about teleporting my child object to it's parent's location without a constant parent? (Parent keeps changing)(c#)(Unity).



Solution 1:[1]

If the child doesn't have any other object between it and the parent, you could do :

child.transform.localPosition = new Vector3(0, 0, 0);

The local position is the position of an object, relative to its parent. So setting it to 0 will make the child go to its parent position.

Solution 2:[2]

You can try this

void UpdateChildParent(Transform newParent, Transform child)
{
  var cPos = child.localPosition; //Save old local position of Child
  child.SetParent(newParent); //Switch to new parent
  child.localPosition = cPos; // copy old local position of child
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Realistic3D