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:

BCW0006: WARNING: Assignment to

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.


You’re looking for this:

        healthBar.sizeDelta = new Vector2(GameGlobal.playerEnergy*300, healthBar.sizeDelta.y);