Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Oct 22, 2013 at 10:01 PM by meat5000 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by rasheedqw · Jul 26, 2013 at 02:02 PM · c#javatranslatehealth

can someone translate this to java I've tried a thousand time but couldn't do it

 public int maxHealth = 100;
   public int curHealth = 100;    
 
 public float healthBarLength ;
 
 // Use this for initialization
 void Start () {
     healthBarLength = Screen.width / 2;
 }
 
 // Update is called once per frame
 void Update () {
     AddjustCurrentHealth(0);
 }

  void OnGUI() {
         GUI.Box(new Rect(10, 10, healthBarLength, 20), curHealth + "/" + maxHealth);
 }
 
 public void AddjustCurrentHealth(int adj) {
         curHealth += adj;
 
          if(curHealth < 0)
              curHealth = 0 ;
          
          if(curHealth > maxHealth)
              curHealth = maxHealth;
          
          if(maxHealth < 1)
              maxHealth = 1 ;
          
          healthBarLength = (Screen.width / 2) * (curHealth / (float)maxHealth);
      }}
Comment
Add comment · Show 6
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image amphoterik · Jul 26, 2013 at 02:03 PM 0
Share

Show us the code that didn't work

avatar image robertbu · Jul 26, 2013 at 02:17 PM 0
Share

I agree with @rasheedqw. If you post your attempt, we will be glad to help you fix up any problems. Here is a link to the syntax differences between the two languages:

http://answers.unity3d.com/questions/12911/what-are-the-syntax-differences-in-c-and-javascrip.html

avatar image aldonaletto · Jul 26, 2013 at 02:48 PM 0
Share

@robertbu and @amphoterik are right: you should be more specific and tell us what's the problem. But you're lucky: I suspected that the problem is the (float) typecasting and posted an answer with some alternatives.

avatar image LemonLake · Jul 26, 2013 at 03:04 PM 0
Share

This is pretty much Java, with a few changes. I believe you mean JavaScript?

avatar image rasheedqw · Jul 26, 2013 at 09:00 PM 0
Share

sorry this is the part that didnt work public void AddjustCurrentHealth(int adj) { curHealth += adj;

      if(curHealth < 0)
       curHealth = 0 ;
 
     if(curHealth > maxHealth)
       curHealth = maxHealth;
 
     if(maxHealth < 1)
       maxHealth = 1 ;
 
     healthBarLength = (Screen.width / 2) * (curHealth / (float)maxHealth);
  }}

im new to unity and translated most of it but i kept getting the error

: The best overload for the method 'Playerhealthbar.AddjustCurrentHealth()' is not compatible with the argument list '(int)'.

wasnt sure it i wrote it wrong or what

Show more comments

1 Reply

  • Sort: 
avatar image
1
Best Answer

Answer by aldonaletto · Jul 26, 2013 at 02:41 PM

I suppose that you're having a hard time trying to find the JS equivalent to (float) - well, it doesn't exist! It's used in this C# code to force a float division (it would otherwise be an integer operation, probably resulting in 0 or 1). JS does value type conversion automatically when needed, but this doesn't help in this case because the compiler can't suspect that you want a float division. A simple solution is to declare a temporary float variable and assign the divisor or dividend to it, then use the float in the division - like this:

 public var maxHealth: int = 100;
 public var curHealth: int = 100;   
 public var healthBarLength: float;
  
 function Start() {
     healthBarLength = Screen.width / 2;
 }
  
 function Update() {
     AddjustCurrentHealth(0);
 }
  
 function OnGUI() {
     GUI.Box(Rect(10, 10, healthBarLength, 20), curHealth + "/" + maxHealth);
 }
  
 function AddjustCurrentHealth(adj: int) {
     curHealth += adj;
     if(curHealth < 0)
         curHealth = 0 ;
     if(curHealth > maxHealth)
         curHealth = maxHealth;
     if(maxHealth < 1)
         maxHealth = 1 ;
     // create a float version of maxHealth and use it in the division:
     var maxHealthF: float = maxHealth; 
     healthBarLength = (Screen.width / 2) * (curHealth / maxHealthF);
 }

A yet simpler solution would be to rewrite the expression like this:

     healthBarLength = 0.5 * Screen.width * curHealth / maxHealthF;

The magic here is done by the 0.5 float constant and the operand order: Screen.width (an int) is multiplied by the float 0.5, resulting in a float value which forces float operations in the next multiplication and, finally, in the division.

Another way to force float conversion of int values is to multiply the value by 1.0 - this doesn't modify the value, but convert it to float:

     healthBarLength = (Screen.width / 2) * (curHealth / (1.0 * maxHealthF));

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AlucardJay · Jul 26, 2013 at 04:40 PM 0
Share

What about parseFloat and parseInt?

 (float)maxHealth => parseFloat( maxHealth )
 (int)someVar => parseInt( someVar )

(still an excellent answer, upvoted)

avatar image aldonaletto · Jul 26, 2013 at 04:50 PM 0
Share

@alucardj, parseFloat doesn't require a string argument? The .NET parseFloat method expects a string, but maybe Unity JS has its own implementation (can't test this right now).

avatar image AlucardJay · Jul 26, 2013 at 05:07 PM 1
Share

Wish I had a technical answer for you, can only say that I've been using this since I started converting C# to uJS, with no problems and the expected result. Here's a test :

 #pragma strict
 
 var myFloat : float = 1.725;
 
 function Start() 
 {
     Debug.Log( myFloat );
     // 1.725
     // UnityEngine.Debug:Log(Object)
     // ParseIntAndFloat:Start() (at Assets/ParseIntAndFloat/ParseIntAndFloat.js:7)
     
     Debug.Log( parseInt( myFloat ) );
     // 1
     // UnityEngine.Debug:Log(Object)
     // ParseIntAndFloat:Start() (at Assets/ParseIntAndFloat/ParseIntAndFloat.js:12)
     
     
     var currHealth : int = 12;
     var maxHealth : int = 18;
     
     Debug.Log( currHealth / maxHealth );
     // 0
     // UnityEngine.Debug:Log(Object)
     // ParseIntAndFloat:Start() (at Assets/ParseIntAndFloat/ParseIntAndFloat.js:21)
     
     Debug.Log( parseFloat( currHealth ) / parseFloat( maxHealth ) );
     // 0.6666667
     // UnityEngine.Debug:Log(Object)
     // ParseIntAndFloat:Start() (at Assets/ParseIntAndFloat/ParseIntAndFloat.js:26)
 }
avatar image aldonaletto · Jul 26, 2013 at 07:39 PM 0
Share

That's a good thing to learn - I used to think that parseFloat/Int only accepted string arguments. Thanks for the hint!

avatar image rasheedqw · Jul 26, 2013 at 09:02 PM 0
Share

thanks i really appreciate the help will try to be more clear on the error next time

Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Help Trying to translate cs to js problem with one block of code 2 Answers

I made my code so that if i hit an enemy its health will go down but when i hit the enemy the health doesnt go down. please help 1 Answer

Health Help D: 2 Answers

[Solved] Enemy Won't Affect Player Health? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges