Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by JustinEllis · Jul 01, 2017 at 08:03 AM · 2dui2d gameui imagerecttransform

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image JustinEllis · Jul 04, 2017 at 06:31 AM 0
Share

This is what ive gotten with youre code

error CS0200: Property or indexer `UnityEngine.RectTransform.rect' cannot be assigned to (it is read-only)

avatar image Bunny83 JustinEllis · Jul 06, 2017 at 05:36 AM 0
Share

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.

avatar image JustinEllis Bunny83 · Jul 11, 2017 at 03:41 PM 0
Share

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.

avatar image JustinEllis · Jul 12, 2017 at 05:22 AM 0
Share

Turns out you also cant modify the sizeDelta

avatar image
0

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);
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image JustinEllis · Jul 04, 2017 at 03:38 AM 0
Share

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?

avatar image
0

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);
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

155 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Accessing RectTransform Rotation? 1 Answer

Displaced UI RectTransform collider in some resolutions issue. 1 Answer

How to animate UI rect transform which works for all resolutions 1 Answer

Unity UI Button distored 1 Answer

Unity 2D not rendering images in Canvas 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges