Search Unity

help convert js to c#

Discussion in 'Scripting' started by hellcaller, May 18, 2011.

  1. hellcaller

    hellcaller

    Joined:
    May 19, 2010
    Posts:
    378
    Need help to convert Jcsript to C#, if there are some experienced C# proggers pls help, here the script:
    Code (csharp):
    1.  
    2. var maximumHitPoints = 10.0;
    3. var hitPoints : float = 10.0;
    4. var healthGUI : GUITexture;
    5. var die : AudioClip;
    6. var buttonDown : boolean = false;
    7. var damage : float = 0.0;
    8.  
    9. private var healthGUIWidth = 0.0;
    10.  
    11. function Awake () {
    12.    healthGUIWidth = healthGUI.pixelInset.width;
    13. }
    14.  
    15. function Update() {
    16.  if (Input.GetButton("Fire1"))
    17.  {
    18.   damage = Time.deltaTime;
    19.   ApplyDamage();
    20.  }
    21.  else
    22.  {
    23.    hitPoints += Time.deltaTime*0.5;
    24.  }
    25. hitPoints = Mathf.Clamp(hitPoints, 0, maximumHitPoints);
    26. }
    27.  
    28. function LateUpdate () {
    29.    UpdateGUI();
    30. }
    31.  
    32. function ApplyDamage () {
    33.    hitPoints -= damage;
    34. }
    35.  
    36. function UpdateGUI () {
    37.    
    38.    var healthFraction = Mathf.Clamp01(hitPoints / maximumHitPoints);
    39.    healthGUI.pixelInset.xMax = healthGUI.pixelInset.xMin + healthGUIWidth * healthFraction;
    40.  
    41. }
     
    Last edited: May 18, 2011
  2. cemC

    cemC

    Joined:
    Dec 23, 2010
    Posts:
    214
    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".
     
  3. hellcaller

    hellcaller

    Joined:
    May 19, 2010
    Posts:
    378
    Here's my variant:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class powerBar : MonoBehaviour {
    6. public float maximumHitPoints = 10f;
    7. public float hitPoints = 10f;
    8. public GUITexture healthGUI;
    9. public bool buttonDown = false;
    10. public float damage  = 0f;
    11. private float healthGUIWidth = 0f;
    12.  
    13.     // Use this for initialization
    14.     void Awake () {
    15.         healthGUIWidth = healthGUI.pixelInset.width;
    16.     }
    17.  
    18.     void Start () {
    19.    
    20.     }
    21.    
    22.     // Update is called once per frame
    23.     void Update () {
    24.         if (Input.GetButton("Jump"))
    25.          {
    26.           damage = Time.deltaTime;
    27.          }
    28.              else
    29.              {
    30.                hitPoints += Time.deltaTime*0.5f;
    31.              }
    32.         hitPoints = Mathf.Clamp(hitPoints, 0f, maximumHitPoints);
    33.    
    34.     }
    35.    
    36. void LateUpdate () {
    37.    UpdateGUI();
    38. }
    39.  
    40. void ApplyDamage () {
    41.    hitPoints -= damage;
    42. }
    43.  
    44. void UpdateGUI () {
    45.    
    46.    float healthFraction = Mathf.Clamp01(hitPoints / maximumHitPoints);
    47.    healthGUI.pixelInset.xMax = healthGUI.pixelInset.xMin + healthGUIWidth * healthFraction;
    48.  
    49. }
    50.    
    51. }
    52.  
    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
     
    Last edited: May 18, 2011
  4. hellcaller

    hellcaller

    Joined:
    May 19, 2010
    Posts:
    378
    Does it mean that i should declare pixelInset in C# as a variable?
     
  5. DallonF

    DallonF

    Joined:
    Nov 12, 2009
    Posts:
    620
    No... something in C# gets funny with structs and multi-level assignments. I think you need something along the lines of...

    Code (csharp):
    1. var tempInset = pixelInset;
    2. tempInset.xMax = 2; //put your calculations here
    3. healthGUI.pixelInset = tempInset;
     
  6. hellcaller

    hellcaller

    Joined:
    May 19, 2010
    Posts:
    378
    i've found smth like this:
    Code (csharp):
    1.  
    2. void UpdateGUI () {
    3.         float healthFraction = Mathf.Clamp01(hitPoints / maximumHitPoints);
    4.         Rect pos = healthGUI.pixelInset;
    5.         pos.xMax = healthGUI.pixelInset.xMin + healthGUIWidth * healthFraction;
    6.         healthGUI.pixelInset = pos;
    7.     }
    8. }
    9.  
     
    Last edited: May 18, 2011