- Home /
This post has been wikified, any user with enough reputation can edit it.
Question by
Peris · Apr 12, 2015 at 09:32 PM ·
unity 5uiguirecttransform
Scaling image width in script error
Hey guys
I'm trying to create a healthbar with the new unity UI system. For this i just need to scale the width of my image using the health value.
This is my code:
var HealthBar : RectTransform;
function Update ()
{
HealthBar.rect.width = GameGlobal.playerEnergy*300;
}
However, when I run the game, nothing happens (it doesnt scale the width), and in the console I get this error:
Assets/_Scripts/core/GUI_GameHUD.js(28,16): BCW0006: WARNING: Assignment to temporary.
Anyone have any idea what I'm doing wrong? I've tried giving it various values, and also tried rect.Set(), but just can't get it to work.
Thanks!
Comment
Best Answer
Answer by DoTA_KAMIKADzE · Apr 13, 2015 at 03:40 PM
You're looking for this:
healthBar.sizeDelta = new Vector2(GameGlobal.playerEnergy*300, healthBar.sizeDelta.y);
Thank you! That worked :). Wish it was more obvious in the docs.