- Home /
How do I scale the Xmax value of a RectTransform
Im trying to create a status bar that scales to the amount of points you put in it. Everything is good so far except for the actual scaling part. This is what I have so far
float t = armorSlider.GetComponent<RectTransform> ().rect.xMax;
t = (t / 8) * MainControl.instance.ArmorSliderState;
armorSlider.GetComponent<RectTransform> ().rect.xMax = t;
However it gives me this error:
Cannot modify a value type return value of `UnityEngine.RectTransform.rect'. Consider storing the value in a temporary variable
I understand the problem, im just not entirely sure how to make a temporary variable for something like this. I just need to figure out how to edit the Xmax value. Any help is greatly appreciated.
Answer by Bunny83 · Jul 04, 2017 at 06:10 AM
The "rect" property of the RectTransform is of type Rect which is a struct. You need a temporary copy of the Rect value in order to change it and then assign it back:
RectTransform rt = armorSlider.GetComponent<RectTransform>();
Rect tmp = rt.rect;
tmp.xMax *= MainControl.instance.ArmorSliderState / 8;
rt.rect = tmp;
This should do what you want.
This is what ive gotten with youre code
error CS0200: Property or indexer `UnityEngine.RectTransform.rect' cannot be assigned to (it is read-only)
Yes, sorry i barely used the new UI system -.- The rect property is actually readonly. It can't be set at all. What you may want to set ins$$anonymous$$d is the offset$$anonymous$$ax value. However keep in $$anonymous$$d that those values are not relative to the button itslef but to it's parent anchor positions offset$$anonymous$$ax is the +- offset from the parents max anchor pos and offset$$anonymous$$in from the $$anonymous$$ anchor pos. Changing offset$$anonymous$$ax will actually change the "sizeDelta" Vector2 which actually specifies the size of the element as well as adjusting the anchoredPosition if necessary. If the anchoredPosition is changed depends on the position of the pivot.
If you just want to scale a RectTransform along it's left edge you should set the pivot onto the left edge (pivot x should be 0) and just change sizeDelta.x. Usually the pivot is set to 0.5, 0.5, that's why the button scales around the center.
Ill mark your answer as correct as that seems to make sense. I havent tried it yet but im sure I could figure it out.
Answer by TheDJBuntin · Jul 03, 2017 at 07:34 PM
RectTransform.rect returns a struct copy of rectTransform's, and you are trying to setting the value on that copy only. So it wont work.
See this: https://forum.unity3d.com/threads/setting-top-and-bottom-on-a-recttransform.265415/
Edit:
Alternative approach is to do:
RectTransform rt = GetComponent<RectTransform>();
rt.sizeDelta = new Vector2(t, 20.0f);
Im looking to only scale the right side of the x axis. So the left side stays in the same position but the right side scales based on the value. Is there any way I can do that?
Answer by JustinEllis · Jul 12, 2017 at 05:40 AM
If anyone's wondering, I got it to work. I actually just decided I wasn't going to mess around with the RectTransform. I set the pivot to the far left center and then I just did this.
float x = armorSlider.transform.localScale.x;
float y = armorSlider.transform.localScale.y;
x = (x / 8) * MainControl.instance.ArmorSliderState;
armorSlider.transform.localScale = new Vector3 (x, y, 1);