• 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 Mybabyhate · Sep 04, 2013 at 06:47 AM · healthbar

Help me fix my health bar script

**When the current health bar (GUI.Box) equal 0 it's outside the health bar, please help me to fix it into the health bar please!!

alt text**

     using UnityEngine;
     using System.Collections;
      
     public class Playerhealth : MonoBehaviour {
         //public GUISkin MetalGUISkin;
         public int maxHealth = 200;
         public int curHealth = 100;
         public int maxExp = 200;
         public int curExp = 100;
         public int LV = 5;
         public static bool gender = false;
         public string Status = "Normal";
         
         public float healthBarLength;
         public float healthBar;
         public float expBarLength;
         // Use this for initialization
     void Start () {
         healthBarLength = 300;
         
         }
        
         // Update is called once per frame
     void Update () {
         AdjustCurrentHealth(0);   
         }
         
        
     void OnGUI(){
         //GUI.skin = MetalGUISkin;
         if (gender == true){
             GUI.Label(new Rect(80,10,healthBar, 20), "Gender: Male");
         }
         else 
             GUI.Label(new Rect(80,10,healthBar, 20), "Gender: Female");
         GUI.Label(new Rect(10,10, healthBar, 20), "LV: " + LV); 
         GUI.Label(new Rect(10,40, healthBar, 20), "HP: "); 
         GUI.Label(new Rect(200,10, healthBar, 20), "Status: " + Status); 
         GUI.Box(new Rect(40, 40, healthBar, 20), curHealth+ "/" + maxHealth);
         GUI.Box(new Rect(43, 42, healthBarLength - 5, 15), "");
         GUI.Label(new Rect(10,80, healthBar, 20), "Exp: "); 
         GUI.Box(new Rect(40, 80, healthBar, 20), curExp + "/" + maxExp);
         GUI.Box(new Rect(43, 82, expBarLength - 5, 15), "");
         }
     public void AdjustCurrentHealth(int adj) {
             curHealth += adj;
            
             if(curExp < 0)
              curExp = 0;
             
             if(curExp > maxExp)
                 curExp = maxExp;
         
             if(maxExp < 1)
                 maxExp = 1;
         
             if(curHealth < 0)
              curHealth = 0;
             
             if(curHealth > maxHealth)
                 curHealth = maxHealth;
         
             if(maxHealth < 1)
                 maxHealth = 1;
            
             healthBarLength = 300 * (curHealth / (float)maxHealth);
             healthBar = 300;
             expBarLength = 300 * (curExp / (float)maxExp);
         }
     }


untitled-1.jpg (43.1 kB)
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

2 Replies

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

Answer by Hoeloe · Sep 04, 2013 at 08:16 AM

Your issue is here, I think:

 GUI.Box(new Rect(43, 42, healthBarLength - 5, 15), "");

You're drawing the bar with a width equal to the calculated health bar length, minus 5. So what happens when the health bar length is 0? Well, obviously, it tries to draw a box with a width of -5, which projects it to the left instead of the right, causing the effect you're seeing. What you really need to do is factor in the difference of 5 here:

             healthBarLength = 300 * (curHealth / (float)maxHealth);

Which should ensure the length never drops below 0.

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 Mybabyhate · Sep 05, 2013 at 06:22 AM 0
Share

I think the images of the box make this, because the box had 3 part with three images: left, right, mid. So if the length = 0 the right part will be on the left and the same of the other part. Three parth only in the same place when the length = 1.

I think solution to fix thix should be disappear the box but still keep the value length when the length = 0. But I'm not know how to do this. Can u help me?

Thanks for your reply!!

avatar image VioKyma · Sep 05, 2013 at 06:26 AM 0
Share

A simple if statement should do this for you. if(health != 0) { //draw box }

avatar image Mybabyhate · Sep 05, 2013 at 06:29 AM 0
Share

But how i disappear the box but still keep appear the value of the box??

avatar image VioKyma · Sep 05, 2013 at 06:59 AM 0
Share

I'm confused by what you mean here. You are not displaying a value in the box. To be clear, the box you need to put in the if statement is as below:

 if (curHealth > 0)
 {
     GUI.Box(new Rect(43, 42, healthBarLength, 15), "");
 }

this will still leave the line above that is drawing the actual values.

avatar image Mybabyhate · Sep 05, 2013 at 10:40 AM 0
Share

Thanks for your reply, i have fixed it :D

avatar image
0

Answer by Shkarface-Noori · Sep 04, 2013 at 08:17 AM

  1. GUI.Box(new Rect(43, 40, healthBar, 20), curHealth+ "/" + maxHealth);

  2. GUI.Box(new Rect(40, 42, healthBarLength - 5, 15), "");

if it didn't work just comment and i will test and provide fix as soon as i get to my PC

Comment
Add comment · Show 1 · 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 Mybabyhate · Sep 05, 2013 at 06:23 AM 0
Share

I think not the length but the images of the GUI.Box make this problem. Thanks for your reply ^^

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Screen.width ~ HealthBar problem - C# 2 Answers

health regeneration after death 1 Answer

Yet another health bar request 2 Answers

Why the healtbar is moving ? 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