Why does my dynamically instantiated UI element have a different scale than the prefab it was instantiated from?

My prefab has scale 2 but the instantiated version has scale 7. The code is extremely simple, only 3 lines.

GameObject canvas = GameObject.Find("Canvas");
GameObject newLevelBlock = Instantiate(levelBlockPrefab);

This is happening after I switched my Canvas UI Scaling mode to Scale With Screen Size. It does not happen on the other modes. I need UI Scaling mode but also need the correct scale of the element.

As a temporary workaround, I’m saving the scale of the prefab and applying to the object after instantiation and its working fine. I can keep it like this, but I still want to know why this isn’t happening and if I’ve done something wrong.

Create UI From Scripting

In short: set worldPositionStays to false as a second parameter to SetParent.

newLevelBlock.transform.SetParent(canvas.transform, false);