- Home /
Help with translating health script from java to C#
Hi Guys
relatively new to unity and began with java script but now want to change to C#. I previously had a health bar 2d texture set up which would change size with the following javascript code which worked;
var maximumHitPoints = 100.0;
var playerHealth = 100.0;
var damage : int = 50;
var healthGUI : GUITexture;
function LoseHealth () {
playerHealth -= damage;
UpdateGUI();
function UpdateGUI(){
var healthFraction = Mathf.Clamp01(playerHealth / maximumHitPoints);
healthGUI.pixelInset.xMax = healthGUI.pixelInset.xMin + healthGUIWidth * healthFraction;
}
I have changed to the following C# code
using UnityEngine;
using System.Collections;
public class PlayerHealthGuiScript : MonoBehaviour {
public float maxHealth;
public float currentHealth;
public int damageAmount;
public GUITexture healthBar;
private float healthGuiWidth;
void Start () {
healthGuiWidth = healthBar.pixelInset.width;
}
public void LoseSomeHealth () {
currentHealth -= damageAmount;
UpdateGui();
}
void UpdateGui () {
var healthFraction = Mathf.Clamp01(currentHealth / maxHealth);
healthBar.pixelInset.xMax = healthBar.pixelInset.xMin + healthGuiWidth * healthFraction;
}
I now get this compilation error
error CS1612: Cannot modify a value type return value of `UnityEngine.GUITexture.pixelInset'. Consider storing the value in a temporary variable
Any help greatly appreciated
Answer by Bunny83 · Sep 25, 2011 at 03:22 PM
Setting xMax in this case doesn't make much sense since it's much more complicated that way. xMin, yMin, xMax and yMax are just wrapper properties and they work internally (kind of) with x, y, width and height.
Like already been mentioned in JS when you access the member of a value type property it automatically creates a local variable, change the member and assign it back. It might be easier but break the rule of value types since it behaves like a reference type.
All you have to do is:
Rect newInset = healthBar.pixelInset;
newInset.width = healthGuiWidth * healthFraction;;
healthBar.pixelInset = newInset;
***********************************
ps just in case someone wants to know how xMax and xMin are implemented:
public float xMax
{
get
{
return this.m_Width + this.m_XMin;
}
set
{
this.m_Width = value - this.m_XMin;
}
}
public float xMin
{
get
{
return this.m_XMin;
}
set
{
float xMax = this.xMax;
this.m_XMin = value;
this.m_Width = xMax - this.m_XMin;
}
}
Answer by Reiv3r · Sep 25, 2011 at 03:04 PM
int maximumHitPoints = 100;
int playerHealth = 100;
float healthGUIWidth;
GUITexture healthGUI;
void LoseHealth(int damage) {
if(playerHealth - damage > 0)
playerHealth -= damage;
else
playerHealth = 0;
}
void OnGUI() {
float healthFraction = Mathf.Clamp01(playerHealth / maximumHitPoints);
Rect rect = new Rect();
rect.xMin = healthGUI.pixelInset.xMin;
rect.xMax = healthGUI.pixelInset.xMin + healthGUIWidth * healthFraction;
healthGUI.pixelInset = rect;
}
Try this. Modify as you see fit.
Answer by Itaden · Sep 26, 2011 at 01:12 AM
Thanks all of you I tried all your solutions and they seem to work fine. Not only that but I now have a better understanding of what was going wrong as well which will help in the future.
I have another 7 scripts to convert so you may hear from me again :)
Your answer
Follow this Question
Related Questions
c# to JavaScript how? 3 Answers
How to fix this for c#? 1 Answer
Need this translated. Js to C#. 2 Answers
[SOLVED] Camera jitters while moving towards/away from player 2 Answers
Translate not working properly 1 Answer