Bug: when you set a parent, the child should not acquire the scale of the parent

I did file a bug report for this, but I was told that it is by design.

Not sure how can it be by design, to see an object changing his values for x, y and z in the scale parameters, just adding it as child of another object?

You parent a ball to a hand; the hand has scale different from the ball; once that the ball is un-parented to the hand, the ball retain the scale of the parent. This is really annoying if you plan to use parenting with game objects that are not the same scale of the parent.

just as simple example, if you want to swap orientation on objects that are not symmetrical (for example an L shaped desk, a guitar with the body carved on one side only and so on), and can’t just rotate it because front and back are different, you can do so by changing scale to -1, which will flip the mesh in a specular way.

Now if you parent another object, and then un-parent it, the child object, once un-parented, will retain the -1 on the scale parameter, looking all messed up.

As much as I am aware that is not common to change the scale for Game Objects, I find disappointing that I am told “this is not a bug”; to me the object, once un-parented, should retain its original parameters.

It isn’t a bug. You are just not doing it correctly. When you parent in the scene, the child adjusts its scale to maintain the scale you see. The reverse is true when unparenting. But that is only in when in the editor.

To properly do what you want and maintain its transform values, you need to use SetParent() and set the worldPositionStays value to true. That will set and unset the parent and not change the values in the transform of the child. If the scale is 3, it will stay 3 when parented and stay 3 when unparented.

Wait, I am aware that the scale of an object set as child, will change; to compensate the ratio of the mesh; but this should happen in the backend; so when you un-parent the object; it should return to have 1,1,1 as values…but it does not.

You mention “in the editor”; so while at runtime, un-parenting won’t restore the scale value automatically? That’s probably the reason why I am having issues.

I did resolve now with another line when un-parenting, that set the scale back to 1; but I will try to do as you suggested.
Thanks for the suggestion.

It won’t return to 1,1,1 unless it was 1,1,1 before it was parented. When you unparent, it just inverses the scale of the parent. if the parent is .5, and the child is 2, the child is converted to 4. When unparented, it will return to 2. Assuming there wasn’t a change in the scale of the parent.

runtime it will convert the scale if you use SetParent([transform],false). In editor it always behaves as SetParent([transform],false), at runtime you can control how it behaves.

From what you say, there is an issue then: the object before being parented, is 1,1,1; and it won’t return to 1,1,1 unless I set it explicitly with

item.transform.localScale = Vector3.one;

The parent stay the same size, from the scale perspective, so nothing change on that aspect.

It is easy to reproduce; my parent object is a GO with 2 mesh (one parent, one child); the parent is set to 1,1,-1; the child is 0.9, 0.9, 0.9.
The child that I add at runtime is 1,1,1; I add the child with

item.transform.parent = parentobject.transform;
item.transform.forward = parentobject.transform.forward;
item.transform.localposition = Vector3.zero;

And when I un-parent it I use

item.transform.parent = null;

You are rotating after parenting it. when you modify the transform after you parent it, it is changed the values.

Just to be very clear, when you “un-parent”, it doesn’t ‘return’ to a previous value, it inverts the value based on the parent to maintain the visual appearance. An object doesn’t store or remember its previous values, it just applies the inverse of what the parent is when un parenting.

If you have a parent of 2,2,-2, and a child of 1,1,1 you then scale the parent to 1,1,1, and unparent, your child will now be .5,.5,-.5.

I just tried your code above is working correctly. You parent, rotate and set the position. When you unparent, it will appear exactly as it does after those changes. The idea is that when you unparent, the appearance looks exactly the same as when parented. If you swap lines 1 and 2, it will probably be closer to what you want. Not sure why you are setting the forward on it.

If you parent, and change any transform values on either the parent or child, unparenting will have different values than when you parented. If you want to have a specific value, regardless of any changes to parent or child while nested, you will need to store them and apply them when you unparent.

Also, you mention a hand above, are you parenting to an animated object? If so you that may be what you are seeing.

1 Like

Agree, I am aware that I rotate the child after parenting it, I never said otherwise.

So when you remove the child from the hierarchy, it does not return to the original scale. My understanding was that the object knows about local space when parented, and when not in a hierarchy, and handle its scale accordingly.

Which is why I assume that it does not matter what the parent value is; but at this point I have to take in consideration that things are different.

If the parent is never scaled, and has .5 for all 3 axis in scale, parenting an object to it modify the scale of the child, so I expect that to retain the scale proportions, the child when parented, become 0.5 too, so it stay consistent with the parent, proportion wise.
But when you take out the child from the hierarchy; you said that it apply the inverse of the parent; although from your example, the scaling was done when the child is already in the hierarchy. In my case, nothing change when the objects are parented; the parent continue to stay to 0.9, the child is still 1.

I am setting the forward because the item has a forward, so when parenting it, I have to match the forward of its parent.

I was simply expecting that a cube that has scale 1, when parented to a cube that has scale .9, and no scale changes happen, when removed from the hierarchy and placed in the scene, would get back to 1, but as you mentioned, this is not possible because the object is not aware of any of its values, once you change the parenting.

I can manage with code, after parenting and un-parenting; but I still see it as an inconvenient. Would not take much to make the GO aware of its previouos state, and keep 2 sets of scale parameters, for when it is parented or not parented.

Yes, but that is what may be causing your problem. Rotating, or parenting a rotated child to non-uniform parent, can cause skewing. So when unparenting it, it will be off, due to the skewing.

You can view the in editor to see what is happening.
Create an empty object with a scale of 1,1,-1
Create a cube of 2, 2, 2
Drag the cube into the empty object
Rotate the cube a bit in any direction
Unparent

No, if the parent is .5, and you parent a child that is 1, the child would be 2 after parenting. Because that scale is local to the parent. The change is not to stay consistent with the parent, but consistent to the world (or whatever local space it came from.
To get the world scale of a child, you multiply it by its parent. So in that example, 2 * .5 = 1;
To get the local scale, you would divide. So child = 1, parent = .5, the new local scale would be 1/.5 = 2

[quote=“darshie1976, post:7, topic: 625765, username:darshie1976”]
But when you take out the child from the hierarchy; you said that it apply the inverse of the parent; although from your example, the scaling was done when the child is already in the hierarchy. In my case, nothing change when the objects are parented; the parent continue to stay to 0.9, the child is still 1.
[/quote]If nothing changes, (scale, rot, pos) on either parent or child, and they are uniform, you will get out what you put in.

[quote=“darshie1976, post:7, topic: 625765, username:darshie1976”]
I am setting the forward because the item has a forward, so when parenting it, I have to match the forward of its parent.
[/quote]Yes, that is rotating the child. As above, rotating a child in a non-uniform scaled transform, will skew, and yield unwanted results.

[quote=“darshie1976, post:7, topic: 625765, username:darshie1976”]
I can manage with code, after parenting and un-parenting; but I still see it as an inconvenient. Would not take much to make the GO aware of its previouos state, and keep 2 sets of scale parameters, for when it is parented or not parented.
[/quote]Correct, it isn’t difficult to do.

2 Likes