Why is RectTransform.rect is read-only in scripts?

Why Unity does not allow RectTransform.rect property to be changed from script?

The rect is calculated from all the other settings of the recttransform.
Changing it directly would break the layout system.

Because as you can read here on the overview, rect is the calculated rectangle in localspace of the transform. A calculation that depends on two or more input values can not be uniquely inverted.

For example if you have two variables A and B as well as a property C. “C” will just return A + B. Now if A is 3 and B is 5, C will return 8. However setting C gives you a problem as there are many different ways how A and B may be adjusted to get the desired value for C.

Assume we would set C to 12. You could keep A constant and just change B to “9” so 3+9==12. Though you could also keep B constant and change A instead to 7 so 7 + 5 == 12. However there are also almost infinite other solutions to get to 12 when you change both variables.

If the exact behaviour is not clear it’s better to not allow it in the first place. RectTransforms highly depend of the anchor position and the parent’s position / rotation / … Because the actual rect can depend on an absolute size value or a relative scale from the parent there are several ways how to get the desired rect.