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 Aug 31, 2015 at 03:55 PM by ThePokedog1 for the following reason:

The question is answered, right answer was accepted

avatar image
1
Question by ThePokedog1 · Mar 04, 2015 at 01:52 PM · c#uiguiinterface

Health Bar Percentages Problem

Hi! I'm having trouble with my health bar percentage and I don't know why because I divided them and then multiplied them by 100.

Here take a look. alt text (says 50/100 and 0%)

It really doesn't make since to me because if I set it to 100 then it is fine.

Here is the code:

 using UnityEngine.UI;
 using System.Collections;
 
 public class PlayerHealth : MonoBehaviour {
 
     public Image PlayerHealthBar;
     public Text Fraction;
     public Text Percent;
     public int maxHealth = 0;
     public int curHealth = 0;
 
     // Use this for initialization
     void Start () 
     {
         Fraction.text = curHealth + "/" + maxHealth;
         Percent.text = (curHealth/maxHealth) * 100 + "%";
     }
     
     // Update is called once per frame
     void Update () 
     {
 
     }
     
 
 }

And here is the setup of it

alt text

Well anyways hope someone can help, thanks in advance.

player-health-example.png (2.2 kB)
player-health-settings.png (16.9 kB)
Comment
Add comment · Show 3
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 meat5000 ♦ · Mar 04, 2015 at 03:14 AM 0
Share

Try

 ((curHealth/maxHealth) * 100).ToString() + "%";

Also, are you entereing those values in inspector after you click play? The code is in start so it runs pretty much after you've clicked go?

avatar image ThePokedog1 · Mar 04, 2015 at 03:50 AM 0
Share

I tried it but it didn't work still says 0% even though it should say 50%, and no I didn't touch the values after I click play.

avatar image meat5000 ♦ · Mar 04, 2015 at 02:26 PM 1
Share

Your inspector values could be overriding your script. They say 100/50 while the script initialises to 0.

2 Replies

  • Sort: 
avatar image
4
Best Answer

Answer by Baste · Mar 04, 2015 at 02:04 PM

When you do this:

 curHealth / maxHealth

, you're dividing one integer by another integer, and you're getting an integer returned. In C# (and most other languages), integer division rounds down to the closest integer result. So 7 / 3 = 2. In your case, 50/100 should be .5, but it's rounded down to 0. For more information, check out the official docs here.

To fix this, you'll have to force float (decimal) division. The easiest way to do that is to convert one of the values into a float, as float division will be used if either the dividend or the divisor is a float:

 Fraction.text = curHealth + "/" + maxHealth;
 float curHealthFloat = curHealth;
 Percent.text = (curHealthFloat/maxHealth) * 100 + "%";


EDIT: you should also replace these lines:

 public int maxHealth = 0;
 public int curHealth = 0;

with this:

 public int maxHealth;
 public int curHealth

As setting values in the script that you're overriding in the inspector can have weird effects when you start working with prefabs. The default value for integers are 0 anyways, so setting them to 0 doesn't really do anything useful. You're either overriding some value you have set in the inspector, or doing nothing.

Comment
Add comment · 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
2

Answer by curiouspers · Mar 04, 2015 at 04:36 PM

Your max and cur health is int, that's why you get 0, there is no error. 50/100 = 0.5, but only if result is float, you have int result, so it's 0

Use this:

 Percent.text = ((float)curHealth/maxHealth) * 100 + "%"

this will convert curHealth to float, and output will be float

Comment
Add comment · 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

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How to convert unity editor GUI functions to something that can be used in game? 0 Answers

How to scale triangle created by GL(using viewport position) with scale factor of Canvas? 0 Answers

Converting OnGUI function to UI 1 Answer

How do I add something to the OnClick of a UI Button from an Editor-Script? 0 Answers

Multiple Cars not working 1 Answer


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