I’m making a GUI like a wristwatch. The parent object contains the back face and a child at the 12 o’clock position. At runtime, 11 clones are 'instantiate’d and given the appropriate rotation.
The problem is that I would like to resize the entire setup, which I do at design time by setting the transform’s scale to 2,2,2 in the inspector.
The back face and original child have been correctly doubled in size, as expected. However, the 11 that were generated by code have failed to inherit this doubling in size.
as can be seen, looking at the transform shows a scale of 0.5 0.5 0.5.
Here is the script that generates the clones:
_Slice [] slices = new _Slice[ 12 ];
// - - -
void Awake( )
{
// register ourselves as a listener for explosion event -- when the user shatters a gem, this fires
_MicToneme.explodeEvent += ExplodeHandler;
tonemeTapEvent += InternalTapHandler;
for( int i=0; i < 12; i++ )
{
_Slice S = ( i == 0 ) ? sliceTemplate : (_Slice)Instantiate( sliceTemplate ); // , Vector3.zero, Quaternion.identity ); <-- tried this
// S.transform.localScale = Vector3.one; <-- and this
// ... but no luck :|
S.name = "X_Slice_" + i;
S.transform.parent = transform;
S.transform.localPosition = Vector3.zero;
Quaternion q = Quaternion.AngleAxis( 30 * i, Vector3.up );
S.transform.rotation = q * S.transform.rotation;
S.SetupForToneme( i );
slices[ i ] = S;
}
}
I am completely at a loss to understand this. I don’t even know what literature to read to better understand what goes on under the hood here. Can anyone bail me out?
It’s because all you’re doing is instantiating a prefab and then attaching it to a scaled parent. That doesn’t apply the parent’s scale to the new GameObject, it just sets the parent reference in its Trasnform component. In fact, I’m pretty sure Unity takes active steps to preserve the current scale when you change parenting.
It’s pretty easy to fix, though. All you need to do is set localScale on your new GameObject’s transform to (1, 1, 1), which will ensure it uses its parent’s scale. If your 12 o’clock object isn’t scaled at (1, 1, 1) you might need to apply it’s scale to the new objects, though.
As for where to read to learn more, its scene graphs and transform hierarchies you want to look at here.
Well, a newly instantiated world object always appears at (1, 1, 1) scale, because that’s how they’re instantiated. Changing it from that is up to you.
As for scale preservation in re-parenting, either the world or local scale must be preserved for things to make any sense. Which way to go really depends on the design of the scene graph, but in general it seems to make more sense to preserve the world so that things stay the same size unless someone explicitly changes them. (Think about a platforming game - if I parent a character to a platform so that they move together and that platform happens to be scaled, I probably don’t want my character to change size as a result.)
As such, it’s getting instantiated at (1, 1, 1), and you’re re-parenting it which preserves that (1, 1, 1) in world space. In this case is not the behaviour you desire, so you then need to set the desired scale in local space.