Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

A little help "translating" from JS to C#

Discussion in 'Scripting' started by SirMarley, Aug 31, 2014.

  1. SirMarley

    SirMarley

    Joined:
    Jul 26, 2014
    Posts:
    115
    Hey guys...

    I wonder if anyone could help me writing this in C#, instead of Java...
    It is wokring fine in Unity, but when I try to compile it for the Metro Store, it gives me an error so I want to have it in C# so I can have everything under the same folder.

    This is the JS script:

    Code (JavaScript):
    1. var hp : float;
    2. var maxHP : float;
    3. var healthBarWidth : int;
    4. var myHealthBar : GameObject;
    5. var myhb : GameObject;
    6. var health : BossCB;
    7.  
    8. function Awake(){
    9.     health = this.GetComponent("BossCB");
    10. }
    11.  
    12.  
    13. function Start () {
    14.     healthBarWidth = 20;
    15.     myhb=Instantiate(myHealthBar,transform.position, transform.rotation);
    16.     maxHP = 300;
    17.     hp = maxHP;
    18. }
    19.  
    20. function Update () {
    21.     myhb.transform.position = Camera.main.WorldToViewportPoint (transform.position);
    22.     myhb.transform.position.x -= .05;
    23.     myhb.transform.position.y -= .05;
    24.     myhb.transform.localScale=Vector3.zero;
    25.  
    26.     hp = health._healths;
    27.  
    28.     var healthPercent : float = hp/maxHP;
    29.     if(healthPercent<0){healthPercent=0;}
    30.     if(healthPercent>100){healthPercent=100;}
    31.     healthBarWidth=healthPercent*20;
    32.     myhb.guiTexture.pixelInset=Rect(10,10,healthBarWidth,5);
    33. }
    Thanks!!
     
  2. SirMarley

    SirMarley

    Joined:
    Jul 26, 2014
    Posts:
    115
    I have been translating some and I have this, so far:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlanetBossHealthBarC : MonoBehaviour {
    5.  
    6.     public float hp;
    7.     public float maxHP;
    8.     public int healthBarWidth;
    9.     public GameObject myHealthBar;
    10.     public GameObject myhb;
    11.     private PlanetBoosCB health;
    12.  
    13.     public bool hbCreation=true;
    14.  
    15.     void Awake()
    16.     {
    17.         health = GetComponent<PlanetBoosCB>();
    18.     }
    19.  
    20.     void Start()
    21.     {
    22.         healthBarWidth = 20;
    23.         myhb = Instantiate(myHealthBar, transform.position, transform.rotation)as GameObject;
    24.         maxHP = 3000;
    25.         hp = maxHP;
    26.     }
    27.  
    28.     void Update()
    29.     {
    30.              hp = health._healths;
    31.  
    32.         float healthPercent  = hp/maxHP;
    33.         if(healthPercent<0){healthPercent=0;}
    34.         if(healthPercent>100){healthPercent=100;}
    35.         healthBarWidth=(int)healthPercent*20;
    36.         myHealthBar.guiTexture.pixelInset = new Rect(50, 50, healthBarWidth, 5);
    37.     }
    38. }
    So what I am missing is:

    Code (JavaScript):
    1. myhb.transform.position = Camera.main.WorldToViewportPoint (transform.position);
    2.     myhb.transform.position.x -= .05;
    3.     myhb.transform.position.y -= .05;
    4.     myhb.transform.localScale=Vector3.zero;
    The health bar suppose to appear in the middle of its parent... but there´s nothing showing up yet

    Any ideas?
     
  3. zeman97

    zeman97

    Joined:
    Sep 22, 2012
    Posts:
    53
    Your transform.localScale is set to 0, try setting it to 1 and see if that fixes it.
     
  4. SirMarley

    SirMarley

    Joined:
    Jul 26, 2014
    Posts:
    115
    Having this

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlanetBossHealthBarC : MonoBehaviour {
    5.  
    6.     public float hp;
    7.     public float maxHP;
    8.     public float healthBarWidth;
    9.     public GameObject myHealthBar;
    10.     public GameObject myhb;
    11.     private PlanetBoosCB health;
    12.  
    13.     public bool hbCreation=true;
    14.  
    15.     void Awake()
    16.     {
    17.         health = GetComponent<PlanetBoosCB>();
    18.     }
    19.  
    20.     void Start()
    21.     {
    22.         healthBarWidth = 20;
    23.         myhb = Instantiate(myHealthBar, transform.position, transform.rotation)as GameObject;
    24.         maxHP = 3000;
    25.         hp = maxHP;
    26.     }
    27.  
    28.     void Update()
    29.     {
    30.  
    31.         myhb.transform.position = Camera.main.WorldToViewportPoint(transform.position);
    32. //        myhb.transform.position.x -= .05;
    33. //        myhb.transform.position.y -= .05;
    34.         myhb.transform.localScale = Vector3.zero;
    35.        
    36.  
    37.         hp = health._healths;
    38.  
    39.         float healthPercent  = hp/maxHP;
    40.         if(healthPercent<0){healthPercent=0;}
    41.         if(healthPercent>100){healthPercent=100;}
    42.         healthBarWidth=healthPercent*20;
    43.         myHealthBar.guiTexture.pixelInset = new Rect(50, 50, healthBarWidth, 5);
    44.     }
    45. }
    I have the little bar a little bit to the side of its parent.... but it is not decreasing while its parent gets hit (as it suppose to happen)
     
  5. SirMarley

    SirMarley

    Joined:
    Jul 26, 2014
    Posts:
    115
    I have been debugging... and I found:
    Everything is decreasing each time the parent gets hit, which is right: hp, healthBarWidth and healthPercent.

    However:
    a) The bar does not change dinamically
    b) depending on what I debug, I get different bar sizes:
    hp = full bar
    healthBarWidth = 20
    healthPercent = 1

    How do I make it work the way it suppose to?
     
  6. SirMarley

    SirMarley

    Joined:
    Jul 26, 2014
    Posts:
    115
    Found it... my bad.
    Last line has to be myhb instead of myHealthBar :)