- Home /
Why is Instantiated object's scale changing?
I have very simple code to instantiate a prefab object that is set like so many tutorial examples I am currently following. I am finding however, that my object below has a scale changed to (2, 2, 2), instead of (1, 1, 1). I have to have that last bit of code to fix it, setting the localScale. But, I don't understand why I need to do this.
itemListContainer is a VerticalLayout container. Is the layout script setting my object's below with this scale? Where is it coming from?
public void LoadItems(){
Vector3 position = itemListContainer.transform.position;
Quaternion rotation = itemListContainer.transform.rotation;
foreach (Transform child in itemListContainer.transform) {
GameObject.Destroy(child.gameObject);
}
foreach (GameSpriteData o in spriteList) {
GameObject itm = (GameObject) Instantiate(storeItemPrefab, position, rotation);
itm.transform.parent = itemListContainer.transform;
//itm.transform.localScale = Vector3.one;
}
}
What happens if you just drag the prefab into the scene manually before hitting play. Is the scale of the prefab twice the size of say a cube?
The scale is 1, and same for a cube, when I add it manually. Totally weird.
Well that's my idea gone then don't know if I can help but can you post some screen shots of whats happening even if I can't help someone else might with more info.
I figured it out. All of this is being done on a Canvas, since this is all UI stuff I'm working on, my Canvas Scaler is causing the change. I have it scaling to screen size. Perhaps the 2 scale being applied is compensating for the Canvas Scaler?
Answer by gdbjohnson · Jan 04, 2015 at 07:42 PM
And, I found the solution. "Consider using the SetParent method instead, with the worldPositionStays argument set to false. This will retain local orientation and scale rather than world orientation and scale, which can prevent common UI scaling issues." http://docs.unity3d.com/ScriptReference/Transform.SetParent.html
Thumbs up... I actually was not having this issue until I upgraded from Unity 5.4 to Unity 5.5. I had a Text (RectTransform) and was calling SetParent, placing the UI element under another UI element inside the Canvas. It was something like this: text.transform.SetParent(parent.transform).
Prior to upgrading this worked fine, however specifying the "worldPositionStays" argument to false did the trick. $$anonymous$$aybe the Unity devs set this value to true by default in the latest update?
I know it's an old answer but just for more info.
I used this and still had an issue, figured out the scale was changing on the RectTransform so made sure to set that to 1,1,1 after SetParent. Only showed when the screen size changed.
The new parent of my instantiated GO already uses a scale of (1,1,1) but the new GO still ended up with weird values (11.xx each) and also a completely wrong position. This fixed it! If you want to also move the new GO, do that before setting the parent! If I do it afterwards, the scale and position go back to the same wrong values.
Answer by dacaramo · Jun 20, 2020 at 12:52 AM
Just use the Instantiate() method overload that takes as third parameter the worlPositionStays boolean, instead of using another weird method
I honestly did not think that would do it but it in fact stop the auto scaling issue I was having. Thanks for the help.
Your answer
![](https://koobas.hobune.stream/wayback/20220613175548im_/https://answers.unity.com/themes/thub/images/avi.jpg)