- Home /
Resize object in runtime
I have 4 rigid bodies connected with hinge joints. They are children to an empty parent object.
When I scale the parent at run-time using localScale the relative positions of the child objects get change.
But if I scale it in editor it works out fine.
Can anyone explain why this is happening ?
Could you please explain what you mean by "of the child objects get change(d)" ? Changed in which way? What do you consider wrong with this?
The same goes for "it works out fine." What does fine mean?
I use following code to resize in run time
o =(GameObject)Instantiate(tObj,trans.position,Quaternion.identity);
o.transform.localScale = new Vector3(5,5,5);
This is what happens. The object is rotated differently but you can see that the child objects are cutting into each other. Their relative positions are off ...
The same goes for "it works out fine." What does fine mean?
I can resize the object to any size in editor. It will retain the child object relative posistions
If I can resize object to 5 in editor and it works fine why can't I do the same in run time?
[2]: /storage/temp/9738-object.png
Yes that's exactly what was happening ...
Thanks for suggesting it
Answer by benni05 · Apr 05, 2013 at 01:15 PM
I assume you do the resizing at runtime (the code above) only once in the Awake or Start method, right?
Your problems might have to do with the Rigidbodies you are using. They are part of the Physics engine Unity is offering. You should manipulate them preferably only in a FixedUpdate() method of MonoBehaviour (or as I said in the Awake or Start method before the Update calls are running). Try to set the Rigidbodies to isKinematic = true before resizing and afterwards switch the isKinematic off again and see whether this avoids the problem.
Changed my comment to answer then, feel free to accept it as such. Thank you.
Did this answer work for anyone?
I'm having the exact same problem and I tried what is suggested here with no luck. I'm only changing transform.localScale in the Start method and before I do, I make all affected rigidbodies kinematic. Then apply the scale change. Then make the same rigidbodies non-kinematic.
I've also broken the steps down at runtime to see what happens. When the rigidbodies are kinematic and the scale is changed, the character looks as it should - all of the hinge joints are anchored in the ways that makes the character look right. As soon as the rigidbodies are made non-kinematic, the joints snap back to what appears to be their original locations pre-scaling. Though as was wijesijp's experience, if this scale change is done in the editor, everything works great.
Your answer
Follow this Question
Related Questions
How to resize nested sprite 1 Answer
Asset Automatically Re-Sizing (On it's own) 1 Answer
Smoothly changing scale on an arbitrary mesh - mobile 1 Answer
size of gameobject in unity 1 Answer
[Question]Scaling Limits 1 Answer