- Home /
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
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);
}
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
Can you send me a link where width is a property of RectTransform ? Because I don't think so.
Ahh correct, sorry my bad, see the correction :)
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? ?
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
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.
I doesn't let met change the value of rect.width because it is readonly, this solution doesn't work...
Answer by miyatin · Sep 27, 2015 at 04:54 PM
You can use sizeDelta.
objectRectTransform.sizeDelta = new Vector2(60, objectRectTransform.sizeDelta.y);
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...
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! :)
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...
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);
}
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
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.