• 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 paradoxxx · Mar 21, 2013 at 07:52 AM · playerhealthbar

updating visual changes to player health

Hi guys. I have a question. I have made a healthbar sc`enter code here`ript using JavaScript. However, even though my debuglog shows my damage taken and I can see visual changes when I set my enemy damage really high, how can I edit my script to display damage taken over time, not just display 0hp when the playerhealth is empty?

I will attach my script below:

 #pragma strict
 var hpBar : GUIStyle;
 var bgimage : Texture2D;
 var fgimage : Texture2D;
 
 static var hpDamage : int = 3;
 
 static var playerHealth = 100.0;
 
 function Update(){
 }
 function Start(){
 }
 
 function OnGUI(){
 GUI.BeginGroup(Rect(10,10,256,32));
 GUI.Box(Rect(0,0,256,32)bgimage,hpBar);
 GUI.BeginGroup(Rect(0,0,playerHealth*256,32));
 GUI.Box(Rect(0,0,256,32),fgimage,hpBar);
 GUI.EndGroup();
 GUI.EndGroup();
 }
 
 static function Enemy1Attack1(){
 playerHealth -= hpDamage;
 }

Comment
Add comment · Show 4
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 paradoxxx · Mar 21, 2013 at 08:01 AM 0
Share

Idk what the deal is with the "125's" popping up everywhere and I definitely don't what is going on with my phone but its very hard to type scripts on the site with this thing...also. a snippet from my enemy code:

 if(distance < attack threshold && Time.time > attackTime{
 healthBar.playerHealth -= healthBar.hpDamage;
 attackTime = Time.time + attackRepeatTime;
 Debug.Log(healthBar.hpDamage);
 }
 
 
 /*this is all in the function Update of my enemy script. I know the attack is working because I have no errors and the log is displaying the appropriate amount of damage my healthbar just isn't moving*/
avatar image Benproductions1 · Mar 21, 2013 at 08:46 AM 0
Share

can you edit your question ins$$anonymous$$d of posting a comment? Also can you format your code in the question, so it's readable? If so I can probably answer your question :)

avatar image Seth-Bergman · Mar 21, 2013 at 08:55 AM 0
Share

you're welcome :)

avatar image Benproductions1 · Mar 21, 2013 at 09:07 AM 1
Share

@Seth-Bergman Damn having $$anonymous$$arma is cool :P @paradoxxx GUI.BeginGroup does nothing with the width, it only changes the origin, so doing the playerhealth width thing with the GUI.Box is the way :)

1 Reply

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

Answer by Seth-Bergman · Mar 21, 2013 at 09:41 AM

In other words.. you are over-complicating this.. All you really need is:

 function OnGUI(){
 GUI.Box(Rect(0,0,256,32),bgimage,hpBar);
 GUI.Box(Rect(0,0,playerHealth*2.56,32),fgimage,hpBar);
 }

I also added the decimal (2.56 rather than 256) to fix the scale...

Here's the result:

http://dl.dropbox.com/u/11203897/HealthBarExample.html

Comment
Add comment · Show 3 · 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 paradoxxx · Mar 21, 2013 at 01:32 PM 0
Share

Thank you! Figured it was something small. I will test it out when I get home.. Its an ordeal trying to type a script formatted from an android phone, muchless, getting my phone to accurately recognize text fields.

avatar image paradoxxx · Mar 21, 2013 at 07:05 PM 0
Share

Ok I made the suggested changes. After a delay, the health bar shrinks into the upper left corner. How can I make it so it only is reduced from the right side of the bar?

avatar image Benproductions1 · Mar 21, 2013 at 09:45 PM 0
Share

@paradoxxx Can you post a screenshot of what happens? The code works like it should, maybe it's something else?

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

12 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

Related Questions

How come my Player, Enemy, Animation & HealthSlider won't correspond to one another? 1 Answer

I need help with the player's health points 1 Answer

Weapon Contact 0 Answers

Regain health on GUI 2 Answers

how to add health to your player 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