Bug or user error? Instantiating prefab onto canvas zero's out child object's position?

I have a prefab with a a child object positioned below it. When I drag this prefab into a scene it behaves as expected.

However when I instantiate it all children have their rect transform coordinates set to 0.


I must be doing something wrong, this seems too basic to be a bug :slight_smile:

1 Like

How are you changing the parenting of the object after you instantiate it?

Figured out the issue. Force of habit led me to pass position and rotation parameters on instantiate.

GameObject icon = Instantiate(Resources.Load(“BuildingPlanMaterialIcon”), Vector3.zero, Quaternion.Identity) as GameObject;

Taking those two extra parameters out fixed it. But I’m still confused as to why instantiating the prefab at Vector3.zero would affect the local position of the “QtyFill” child object of the instantiated prefab.

Also I’m just parenting to a layout group gameobject that I have defined on my Master UI script.
icon.transform.parent = selector.MasterUIClass.BuildingPlanMaterialWindow.transform;

Use Transform.SetParent() with the second argument set to false, it is the correct way for GUI elements and we are contempleting removing the other option (or at least deprecating it)

1 Like

I found the SetParent function super useful for implementing a tooltip: I used SetParent(…,false) to position the tooltip relative to a child object, and then used SetParent(…,true) to move it into the top level of the canvas so it would be in the same position but drawn above everything else.