Search code examples
unity-game-enginepositiontransformparent-child

Unity: transform child's position and leave parent where it is


I want to move the itemChild away from the parent, but the parent always move to the new position with the child. Why the parent follow the child's position? How can I move the child and leave the parent where it is. My code:

GameObject go = new GameObject("go");
    go.transform.parent = GameObject.FindGameObjectWithTag("Canvas").transform;
    itemChild.transform.SetParent(go.transform);
    itemChild.transform.localPosition = new Vector2(itemChild.transform.position.x, itemChild.transform.position.y + 250);

Solution

  • Parent should stand still. Are you sure parent is moving with the child ? I mean you are just creating an empty GameObject, can share with us the x,y,z of parent and child ? I tried your code with a bit of change and it is working for me. Here is my code : https://i.sstatic.net/Jnjzj.jpg If you just create an empty game object and click on it in the canvas, since your parent("go" in your case) is empty it will just focus on your child object and it might confuse you.