Need help Translating this to C# please

Hi, iv been trying to translate a piece of code from Unity-Script/Javascript to c# and I’m stuck with one little piece which is this.

var percentofhp = phpcurrent / phpmax;
var hpBarLength = percentofhp * percent;
playerHpBar.guiTexture.pixelInset.width = hpBarLength;

what I know is I need to store this “playerHpBar.guiTexture.pixelInset.width = hpBarLength;” in a temporary variable but I’m not to sure were to start.

so if any one could give me a hand would be much appreciated.
thanks.

honestly… that should work right out the box syntactically. It just happens that code is interchangeable.

You could change the two ‘var’ words to ‘float’. But as long as those variables you’re dividing and multiplying are floats, the ‘var’ will technically work just fine.

(all thoughs values are floats)

I tried this

float percentofhp= phpcurrent / phpmax;
        float hpBarLength= percentofhp * percent;
        playerHpBar.guiTexture.pixelInset.width = hpBarLength;

but then It highlights "playerHpBar.guiTexture.pixelInset.width = hpBarLength; "
with this error…
“unity cannot modify a value return value of unityengine.guitexture.pixelinset.consider storing the value in a temporary variable”

PixelInset is a Rect which is a struct type, so you can’t modify it’s property directly.

Try this:

var r = playerHpBar.guiTexture.pixelInset;
r.width = hpBarLength;
playerHpBar.guiTexture.pixelInset = r;

thank you very much, worked like a charm.