Setting values of a Transform childed to a RectTransform giving unexpected results.

Straight forward code, straight forward problem:

itemGraphics[itemSlot.IndexInList] = (GameObject)Instantiate(itemSlot.Item.ModelAsset);
        itemGraphics[itemSlot.IndexInList].transform.parent = itemSlot.transform;
        itemGraphics[itemSlot.IndexInList].transform.lossyScale.Set(1, 1, 1);
        itemGraphics[itemSlot.IndexInList].transform.rotation = Quaternion.identity;

It’s giving unexpected values in the inspector for scale and rotation. The parent (itemSlot) of the child (itemGraphic) is a worldspace ui element, so it has a RectTransform, where as the child itemGraphic is a regular gameobject with a regular Transform.

I tried editing local scale and local rotation instead; same issue. I tried parenting after setting scale and rotation (tried both local and world) and still same issue.

All I want to do is child a 3D graphic to a 2D UI element.

EDIT: Sceenshots of the output:

After changing scale to 1 and rotation to 0 through inspector at runtime:

EDIT: I just figured something out. It seems to be scaling down the child’s x and y by the parent rectTransform’s width and height. The rectTransforms width and height is 50, so if I scale the child’s x and y to 50, it returns to normal.

I guess if there’s no better way, I can just code it to scale up the child by the parents width and height…

EDIT: ^Actually that still doesn’t work. For some reason the final scale is some crazy high numbers in the thousands. Something’s retransforming it after I do.

Also if I change the values at runtime in the inspector, if the rotation isn’t 0,0,0, the model skews even though the scale doesn’t change.

This is all very strange behaviour, I wish I could fine documentation on how rectTransforms and transforms are supposed to work together.

You can’t set the lossy scale. It’s readonly and all you do is reading the lossyscale and then you change the returned value. Vector3 is a value type. lossyScale is a property which doesn’t have a setter, only a getter. The lossy scale is approximated from the transform hierarchy but depending on the rotation / translation / scale of the parents.

Setting the localScale before parenting should work as Unity adapts the scale to try to maintain the current scale. Once parented the scale might be changed depending on the scale / rotation / translation of the parents.