Need help to convert Jcsript to C#, if there are some experienced C# proggers pls help, here the script:
var maximumHitPoints = 10.0;
var hitPoints : float = 10.0;
var healthGUI : GUITexture;
var die : AudioClip;
var buttonDown : boolean = false;
var damage : float = 0.0;
private var healthGUIWidth = 0.0;
function Awake () {
healthGUIWidth = healthGUI.pixelInset.width;
}
function Update() {
if (Input.GetButton("Fire1"))
{
damage = Time.deltaTime;
ApplyDamage();
}
else
{
hitPoints += Time.deltaTime*0.5;
}
hitPoints = Mathf.Clamp(hitPoints, 0, maximumHitPoints);
}
function LateUpdate () {
UpdateGUI();
}
function ApplyDamage () {
hitPoints -= damage;
}
function UpdateGUI () {
var healthFraction = Mathf.Clamp01(hitPoints / maximumHitPoints);
healthGUI.pixelInset.xMax = healthGUI.pixelInset.xMin + healthGUIWidth * healthFraction;
}
cemC
May 18, 2011, 12:41pm
2
it is not specific code in C#.However , there are some differences between defines functions and variables.
For example, in Jscript you define functions as “function Update or function OnGUI” . C# script , you define functions as “void Update or void OnGUI”.Moreover, for variable definitions in jscript, you define the variables as "var a = 5 or var buttonDown : boolean = false ". In C# script you should define the variables as “int a=5 or bool buttonDown=false”.
Here’s my variant:
using UnityEngine;
using System.Collections;
public class powerBar : MonoBehaviour {
public float maximumHitPoints = 10f;
public float hitPoints = 10f;
public GUITexture healthGUI;
public bool buttonDown = false;
public float damage = 0f;
private float healthGUIWidth = 0f;
// Use this for initialization
void Awake () {
healthGUIWidth = healthGUI.pixelInset.width;
}
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetButton("Jump"))
{
damage = Time.deltaTime;
}
else
{
hitPoints += Time.deltaTime*0.5f;
}
hitPoints = Mathf.Clamp(hitPoints, 0f, maximumHitPoints);
}
void LateUpdate () {
UpdateGUI();
}
void ApplyDamage () {
hitPoints -= damage;
}
void UpdateGUI () {
float healthFraction = Mathf.Clamp01(hitPoints / maximumHitPoints);
healthGUI.pixelInset.xMax = healthGUI.pixelInset.xMin + healthGUIWidth * healthFraction;
}
}
But i’ve got this error:
Assets/Scripts/powerBar.cs(46,14): error CS1612: Cannot modify the return value of `UnityEngine.GUITexture.pixelInset’ because it is not a variable
Does it mean that i should declare pixelInset in C# as a variable?
No… something in C# gets funny with structs and multi-level assignments. I think you need something along the lines of…
var tempInset = pixelInset;
tempInset.xMax = 2; //put your calculations here
healthGUI.pixelInset = tempInset;
i’ve found smth like this:
void UpdateGUI () {
float healthFraction = Mathf.Clamp01(hitPoints / maximumHitPoints);
Rect pos = healthGUI.pixelInset;
pos.xMax = healthGUI.pixelInset.xMin + healthGUIWidth * healthFraction;
healthGUI.pixelInset = pos;
}
}