How to change the size of a UI Image from code? (Trying to make a simple healthbar from this)

Hello, I’m using the new UI system from Unity 4.6 beta…

Tried different codes, looked at the docs and searched around but can’t find the answer…

I created an Image object inside of a UI Canvas. I’m trying to make a vertical health bar using it, so I need to be able to modify the bar’s height at runtime. The new Image object’s height/width is adjusted during development time using its RectTransform component, as you see in the screenshot. But how to modify that at runtime?

I currently have the Image object dragged into a GameObject variable of my main game script and have tried stuff like this (using CSharp):

public GameObject healthbar;
healthbar.GetComponent().rect.Set(0,0,100, 300);

which doesn’t work. And healthbar.GetComponent().rect.y is GET only so cannot be changed at runtime.

I’ve also tried:
healthbar.transform.localScale.y = 15;

which doesn’t work either.

what is the proper way to change size at runtime? You can give me an example in either JS or C#, doesn’t matter.

thanks a lot!

1 Like

My guess is that the rect property of RectTransform is giving you a copy back so Set() is being called on the copy. Try assigning rect to some local variable, Set()ing that, and then re-assigning back to the RectTransform.

This not work, please help, I stuck with that.

1 Like

Find Image component and set requred size:

_image.rectTransform.sizeDelta = new Vector2(width, height);
37 Likes

Thanks for this correct reply. Using this way, we can set size of Image through script.

_image.rectTransform.sizeDelta = new Vector2(width, height);
6 Likes

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class whatEver : MonoBehaviour {

void Update()
{
if(Input.GetKeyDown(KeyCode.R))
{
transform.localscale = new Vector3(2, 2, 2);
}

if(Input.GetKeyUp(KeyCode.R))
{
transform.localscale = new Vector3(1, 1, 1);
}
}

}

3 Likes

I think the solution you want is you want to push the image resolution to the rect transforms width and height?

public RectTransform rt;
public Image image;

public void SomeFunction()
{
rt.sizeDelta = new Vector2 (image.sprite.rect.width,image.sprite.rect.height);
}

Hope I helped anyone!

6 Likes

No, no, no. NEVER use that method to set the size of a RectTransform, it is compeltely wrong. It will sometimes, because of coincidence have a side-effect of setting the size.

See here for the actual correct API method for setting size, and detailed explanation (in the thread) of why this is correct: Modify the width and height of RectTransform - Unity Engine - Unity Discussions

(PS: I hate that someone at Unity chose such a bizarre naming scheme for the API, and deleted/blocked the obvious “size” - but that’s what we have to deal with :))

2 Likes

as posted on the other thread, the call to SetSizeWithCurrentAnchors won’t work?
somehow the size of the gameobject is not changing.

Turned out i’ve searched for the wrong element. it’s now doing something :wink:

I found this video that helped me. Get and Set the Size of UI image Using C# in Unity - Short Version
They also posted the code for use on gitHub

This is completely wrong, you were too lazy to read the thread (which explains IN DETAIL why your post is wrong). Don’t post stuff you don’t understand on threads you didn’t read.

All you’ve done is post an advert for a YouTube video - that is itself badly incorrect.