Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 FlashX · Sep 27, 2015 at 10:10 AM · c#width

Change width of UI Image C#

Hi dudes!!

Argh! I'm back again!

I'm trying to get a simple UI loadbar to change in size. The code ive tried doesnt work obviously and would love your help..

             GameObject theBar = GameObject.Find("Canvas/loadBar");
             RectTransform objectRectTransform = theBar.GetComponent<RectTransform> ();
             objectRectTransform.width(60);

I'm getting this error message but I'm not sure that i understand it...

 Assets/Scripts/Colour.cs(184,45): error CS1061: Type `UnityEngine.RectTransform' does not contain a definition for `width' and no extension method `width' of type `UnityEngine.RectTransform' could be found (are you missing a using directive or an assembly reference?)

what am i doing wrong?

Cheers

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

4 Replies

· Add your reply
  • Sort: 
avatar image
5
Best Answer

Answer by FlashX · Sep 29, 2015 at 05:34 AM

just for those at home wondering

... thanks to @Kornikolia @Eudaimonium

  public float width;
      void Update ()
      {
          GameObject theBar = GameObject.Find ("Canvas/loadBar");
          var theBarRectTransform = theBar.transform as RectTransform;
          theBarRectTransform.sizeDelta = new Vector2 (width, theBarRectTransform.sizeDelta.y);
      }
 
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
avatar image
5

Answer by Eudaimonium · Sep 27, 2015 at 02:11 PM

width is not a method or a function, it's a property, so you'd need to use:

 objectRectTransform.width = 60f;

CORRECTION:

 objectRectTransform.rect.width = 60f;

Apologies, forgot the "rect" part :D

Comment
Add comment · Show 6 · 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 Kornikolia · Sep 27, 2015 at 04:57 PM 0
Share

Can you send me a link where width is a property of RectTransform ? Because I don't think so.

avatar image Eudaimonium Kornikolia · Sep 27, 2015 at 10:19 PM 1
Share

Ahh correct, sorry my bad, see the correction :)

avatar image Kornikolia Eudaimonium · Sep 28, 2015 at 06:53 PM 0
Share

Do you mean this property? I think it will not work due that RectTransform.rect is just a calculated value and not a actual rect, so you can't change it and even if you can do it - it will not affect? ?

avatar image FlashX · Sep 28, 2015 at 06:55 AM 0
Share

Hi Eudaimonium,

Thanks for your response, I'm still having a bit of issue inplementing it though. Ive had a look at the docs and found this:

     public Rect rect = new Rect(0, 0, 10, 10);
     void Example() {
         print(rect.width);
         rect.width = 20;
     }

however, how do i implement that to an image thats manually been put on the canvas?

Thanks again

avatar image Eudaimonium FlashX · Sep 28, 2015 at 09:35 AM 0
Share

I believe you're currently trying to use the code that's in the opening question? If so, then your solution is:

 GameObject theBar = GameObject.Find("Canvas/loadBar");
              RectTransform objectRectTransform = theBar.GetComponent<RectTransform> ();
 objectRectTransform.rect.width = YourNewWidthValue;

First grab the reference to a game object - I would NOT suggest using a .Find function, as it can be quite bad on performance as per official documentation. Setup a public GameObject in your script, and then assign it via Inspector.

Or better yet, setup a public RectTransform object, and assign that. Then simply access the rect.width property when you need it. If you need help with code itself, post what you got on pastebin.com and send me the link over. I'll do my best to correct it for you and explain it.

avatar image adrianfrancisco · Jan 28 at 11:10 AM 0
Share

I doesn't let met change the value of rect.width because it is readonly, this solution doesn't work...

avatar image
1

Answer by miyatin · Sep 27, 2015 at 04:54 PM

You can use sizeDelta.

 objectRectTransform.sizeDelta = new Vector2(60, objectRectTransform.sizeDelta.y);
Comment
Add comment · Show 6 · 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 Kornikolia · Sep 27, 2015 at 05:00 PM 0
Share

I'm not sure, that "sizeDelta" will be the same as "width", because this value "sideDelta" depends on anchors and stretch settings, i.e. anchor$$anonymous$$ax, anchor$$anonymous$$in properties...

avatar image FlashX · Sep 28, 2015 at 06:49 AM 0
Share

hmmm... Thank you for that line of code, i can get it working but it seems to resize both x and y at the same time, is there a way to only resize just the x value? I had a look through the docs but couldn't find anything.

Thanks again! :)

avatar image Kornikolia FlashX · Sep 28, 2015 at 08:09 AM 0
Share

That is what I mean, you should change rect properties to this: [link to Image][1] [1]: https://www.dropbox.com/s/rfqa16jtnep3pgs/RectTransform.png?dl=0 then you can change sizeDelta and it will fill from left to right...

avatar image Kornikolia FlashX · Sep 28, 2015 at 06:59 PM 1
Share

Set values for Rect like here: And use this code (It will update your width each frame, it's not a good practice to code like this, but you will be able to modify it):

 public float width;
     void Update ()
     {
         GameObject theBar = GameObject.Find ("Canvas/loadBar");
         var theBarRectTransform = theBar.transform as RectTransform;
         theBarRectTransform.sizeDelta = new Vector2 (width, theBarRectTransform.sizeDelta.y);
     }


avatar image FlashX Kornikolia · Sep 29, 2015 at 05:33 AM 0
Share

OH $$anonymous$$Y GOSH! I don't know what i was doing wrong! I've finally got it working thanks to both you guys' help! I've used the code above and it works brilliantly!! The only issue i have is that it still $$anonymous$$imises on both sides of the image as if the pivot is at its centre however it's set to the left, fortunately it's actually doing what i want though if you have any ideas as to why the left hand alignment isn't working, i would love to hear.

Thanks for all your help guys!

@$$anonymous$$ornikolia @Eudaimonium

Show more comments
avatar image
0

Answer by Kornikolia · Sep 28, 2015 at 06:20 AM

Hi! I would say, there is no width property in RectTransform. But you can use offsetMax and OffsetMin or you can just change a SizeDelta. what should you use? it depends on layouts, so.. try to play with it, and you can try to check debug values in editor of your image to understand what do you need.

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

31 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Making a bubble level (not a game but work tool) 1 Answer

Changing the size of a text object to only be as long as the letters 0 Answers

An OS design issue: File types associated with their appropriate programs 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