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> ();

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?


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);

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

objectRectTransform.width = 60f;


objectRectTransform.rect.width = 60f;

Apologies, forgot the “rect” part :smiley:

You can use sizeDelta.

objectRectTransform.sizeDelta = new Vector2(60, objectRectTransform.sizeDelta.y);

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.