How to scale rectTransform in script

How can I set scale, or any other properties of rectTransform in runtime?

I found only how to set size via rectTransform.sizeDelta

But for example updating scale like this…
rectTransform.localScale.Set(1 / _lengthHeightRatio, 1.0f, 1.0f);
…does nothing.

Is there a way to change that in running game? As well as other properties of this new rectTransform?

This is not a bug, that’s how C# (or any object oriented language) is supposed to work. Its intended behavior, in fact Transform logs a warning when you attempt to do the same things for it’s properties like position rotation and localScale

localScale is a property of rectTransform, a Struct to be more precise. when you edit localScale.x, you’re editing a value on the Vector, not the RectTransform. because of Encapsulation (specifically where one class shouldn’t manage the data of another class, or in this case a struct) the RectTransform is never directly notified that the vector has changed (it just knows that you accessed it). And Vectors don’t keep a reference to their containing RectTransform (nor should they) so they can’t message them when their internal values change.

you are supposed to manually set the property itself for the rectTransform to be “allowed” to detect a change. Any of these should work:

//case 1
text.rectTransform.localScale = new Vector(newscale,1f,1f);

//case 2
Vector3 temp = text.rectTransform.localScale;
temp.x = newscale;
text.rectTransform.localScale = temp;

//case 3 never tried this, but should work since localScale is a struct
text.rectTransform.localScale.x = newscale;
text.rectTransform.localScale = text.rectTransform.localScale;

In all cases you’re manually mutating the entire localScale property. This gives the RectTransform the chance to catch the change and update its own state as needed

Ok, solved…

txt.rectTransform.localScale = new Vector3(newScale, 1.0f, 1.0f);//This works

txt.rectTransform.localScale.Set(newScale, 1.0f, 1.0f); //This DOES not. Despicable bug