parenting problem

In my Inventory script attached to my Inventory GameObject I have this:

public void AddItem (GameObject thing){
	invList.Add(thing);
	thing.transform.parent = transform;
}

When I run the game and this function is triggered, the GameObject passed as thing is parented under the Inventory( I can see it doing what I want in scene hierarchy), but there’s a problem. The Inventory, which I have moved to a location the player will never go, (-10 in Y for now) is moving up to where the child is. In the editor it shows the y position being -10 (unchanged), but it has visually moved to where the child is. What’s going on? I want the Inventory parent to remain where it was.

1 Answer

1

Are you sure you’re not looking at the ‘thing’. I hope there’s a better answer, cuz this drives me nuts, but when you reparent something, Unity is ‘nice’ enough to adjust the transforms so that it looks like it’s in the same place in world space. I wish there were another API. Maybe overload ~= or something.

Anyway, after you set the parent, then

thing.localPosition = Vector3.zero;

should fis it for you