- Home /
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.
Answer by DaveA · Mar 27, 2012 at 09:37 PM
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
Ah, you know what? I think it's just Unity's editor that sort of threw me off. WHen I clicked on Inventory in the hierarchy it would show me the transform XYZ arrows of "thing" under it and not the Inventory itself. I guess because it's an empty gameObject. Not how I would want it to function, but you were right. I WAS looking at thing's transform even though I expected to be seeing Inventory's. THAN$$anonymous$$S!