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 /
avatar image
0
Question by pickle chips · Jun 10, 2013 at 03:01 AM · recthealthbarwidthhealth

rect.width gets set to 0 for unknown reason

So i'm having an odd issue with my health bar. To try and decrease health, what i did was use two textures one on top of another. The front is the health, the back is the background

to show a loss of health, I divided the current health and the max health, then multiplied it by the width of the background, so it becomes a percentage of the background width. But in the editor when playing my game, once the current health becomes anything below the max health it automatically goes to width 0, displaying as if there's no health!

Hopefully that makes sense, if not i'll try to elaborate

Here's my code:

     public int maxHealth = 100;
     public int curHealth = 100;
     public Texture2D healthBarFront;
     public Texture2D healthBarBack;
     public float healthBarWidth = 80;
     public float healthBarHeight = 8;
     public float above;
     Rect rect; 
     Rect rectFront;
     public float rectFrontWidth;//This is just used for me to view in the inspector
     
     // Use this for initialization
     void Start () {
         rect.width = healthBarWidth;
         rect.height = healthBarHeight;
         
     }
     
     // Update is called once per frame
     void Update () {
         rectFrontWidth = (float)rectFront.width; //Again, just for inspector
     }
     
     //Drawing healthbar
     public void OnGUI() {
             //finding the position
         Vector3 v3pos;
         v3pos = transform.position;
         v3pos.y += above;
         v3pos = Camera.main.WorldToScreenPoint(v3pos);
         v3pos.y = Screen.height - v3pos.y;
         //Transffering into a Rect (The background)
         rect.x = v3pos.x - healthBarBack.width /2;
         rect.x += xoffest;
         rect.y = v3pos.y - healthBarBack.height /2;

         rectFront = rect;
           //This next line is where the problem is. Width gets set to 0 if curHealth is less than maxHealth
         rectFront.width = (curHealth/maxHealth) * healthBarWidth;

         GUI.DrawTexture(rect, healthBarBack);
         GUI.DrawTexture(rectFront, healthBarFront);
     }
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Eric5h5 · Jun 10, 2013 at 03:49 AM

You're using integer division; either use floats instead of ints, or else cast to float when doing the division.

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 Mukabr · Jun 10, 2013 at 03:52 AM 0
Share

in other words

 rectFront.width = (Float(curHealth)/maxHealth) * healthBarWidth;

or

  rectFront.width = (curHealth/int(maxHealth)) * healthBarWidth;
avatar image bodec · Jun 10, 2013 at 04:57 AM 0
Share

rectFront.width = (float)((curhealth/maxhealth) * healthbarWidth)

avatar image Bunny83 · Jun 10, 2013 at 06:16 AM 0
Share

@$$anonymous$$ukabr: the function style cast operator doesn't exists in C# as far as I know. It belongs to C or C++.

@bodec: Your cast won't help since you just cast the final result which will be 0.

When you combine those two comments you get:

 rectFront.width = ((float)curhealth / maxhealth) * healthbarWidth;

It's all about the division operator. At least one operand need to be a float so the compiler does a float division.

Note: UnityScript doesn't have the c-style cast, so you have to use implicit casting by using a temporal float variable for one of the operands.

avatar image Eric5h5 · Jun 10, 2013 at 06:25 AM 0
Share

Or you can use parseFloat, or you can add 0.0, as in (curhealth + 0.0).

avatar image pickle chips · Jun 10, 2013 at 07:51 PM 0
Share

Oh my god i feel dumb, it was that obvious! the float cast worked perfectly,

 rectFront.width = ((float)curHealth/maxHealth) * healthbarWidth;

Thanks a lot guys! Now my health bar system is all set up!

thanks Eric5h5 for your explanation, and bunny83 for your example!

And thanks to everyone else for your input!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

18 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

Related Questions

Multiple Cars not working 1 Answer

Help With Enemy Health bars 3 Answers

GuiTexture above 3D objects 1 Answer

I made a better shader how do i fix[add _Shadow Strength]help???>Sorry that im asking for to much 1 Answer

Player Heal *Help* 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