I want to scale a GameObject with children from code using the parent pivot. When I do it from the Unity Editor I get the result I want when scaling the parent object.
However, from the code I am scaling the GameObject with localScale like this:
GameObject g = Instantiate(prefab, position, rotation);
g.transform.localScale = new Vector3(3.0f, 3.0f, 3.0f);
The result is as if I would have scaled the child objects one by one (according to the pivots of each child gameobject). I don’t understand why it is happening
Maybe it’s a silly thing but some help is appreciated! Thank you!
Thank you for answering and of course!! I’m sorry if I explained it bad!
I have these two objects:
((See Image 1))
When I scale the Object1 (the one on the left) from the Unity Editor it works as expected:
((See Image 2))
However, now I want to do this for the Object 2 (on the right) from code, so I added a script to the Object2 and typed this line in the Start() method.
transform.localScale = new Vector3(2.0f, 2.0f, 2.0f);
When I play the game, this is the result. On the left we can see the Object1 scaled “well” from the Editor and on the right what the script did.
((See image 3))
What I want is scaling, from code, the ObJect2 exactly how the Unity Editor scaled the Object1
And, just in case, these are the two Objects in the Unity Hierarchy:
((See image 4))
I hope you can understand now