Parenting instantiated prefab breaks scale

I have a canvas prefab (image with children) that is created on runtime.

frame = (GameObject)Instantiate(OverviewPrefab, position, Quaternion.identity);
OverviewList.Add(frame);
frame.transform.parent = InteractibleElements[7].transform;

position is a new Vector3 calculated on the fly, could be 0,0,0

If drag the prefab into the scene, outside of the canvas (screenspace - camera), it become really tiny.
If i then drag it inside the canvas, it gets scaled up to 489, which is roughly 80 times larger than it has to be. (original prefab scale is 1,1,1)
However if i drag the prefab from the project overview directly into the canvas, it shows up with a correct scale as designed.

The problem with my code is that i can’t find a way to instantiate a prefab directly into the canvas, and stuck with this weird scaling issue. (+offset and size)

You’ll notice when you drag it directly into the canvas, the objects local scale changes (probably to something ridiculously tiny like 0.000024124). You need to use SetParent method to avoid this:

frame.transform.SetParent(InteractibleElements[7].transform);

http://docs.unity3d.com/ScriptReference/Transform.SetParent.html

1 Like

Thanks for the reply!.
How do view local scale? In the inspector window Rect Transform Scale stays 1,1,1 no matter where i place it.

The scale for the Main Canvas element however, is 0.002176… and cant be scaled manually

Edit: your solution worked when i set the second parameter from SetParent() to false

2 Likes