- Home /
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);
newLevelBlock.transform.SetParent(canvas.transform);
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.
Answer by Xarbrough · Dec 25, 2015 at 08:09 PM
In short: set worldPositionStays to false as a second parameter to SetParent.
newLevelBlock.transform.SetParent(canvas.transform, false);
Worked like a charm, thanks for the doc references it helped to understand the solution more.
Your answer
Follow this Question
Related Questions
Is there a way to change UI Scale Mode without causing my UI to mess up? 1 Answer
Canvas with GUI elements in prefab act strangely 0 Answers
[4.6 GUI] Displaying my pause menu when ESC is pressed 2 Answers
Use the canvas scaler to resize UI on a RenderTexture's resolution ? 0 Answers
Use Unity UI For 2D Games Or Custom Objects Instead? 2 Answers