Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
2
Question by DayyanSisson · Aug 12, 2011 at 07:53 PM · guitexturedamagegui-buttonhealth

Making a Health Bar with Code

I have a damage reciever script, and a health bar script, but I want the health bar to decrease as the player gets hit. How should I do that? Here are my scripts:

Health Bar GUI Script

 function OnGUI() {
     GUI.backgroundColor = Color.green;
       GUI.Button(Rect(10,640,510,20),"Health");
 }

Damage Reciever Script

 var hitPoints = 100.0; 
 var detonationDelay = 0.0; 
 var explosion : GameObject; 
 var deadReplacement : Rigidbody; 
 
 function ApplyDamage (damage : float) { // We already have less than 0 hitpoints, maybe we got killed already? if (hitPoints <= 0.0) return;
 
 hitPoints -= damage;
 if (hitPoints <= 0.0) {
     // Start emitting particles
     var emitter : ParticleEmitter = GetComponentInChildren(ParticleEmitter);
     if (emitter)
         emitter.emit = true;
 
     Invoke("DelayedDetonate", detonationDelay);
 }
 }
 
 function DelayedDetonate () { BroadcastMessage ("Detonate"); }
 
 function Detonate () { // Destroy ourselves Destroy(gameObject);
 
 // Create the explosion
 if (explosion)
     Instantiate (explosion, transform.position, transform.rotation);
 
 // If we have a dead barrel then replace ourselves with it!
 if (deadReplacement) {
     var dead : Rigidbody = Instantiate(deadReplacement, transform.position, transform.rotation);
 
     // For better effect we assign the same velocity to the exploded barrel
     dead.rigidbody.velocity = rigidbody.velocity;
     dead.angularVelocity = rigidbody.angularVelocity;
 }
 
 // If there is a particle emitter stop emitting and detach so it doesnt get destroyed
 // right away
 var emitter : ParticleEmitter = GetComponentInChildren(ParticleEmitter);
 if (emitter) {
     emitter.emit = false;
     emitter.transform.parent = null;
 }
 Destroy (gameObject);
 }
 
 // We require the barrel to be a rigidbody, so that it can do nice physics @script RequireComponent (Rigidbody)

The damage reciever script works. All I need is the damage done using the damage reciever to affect the health bar (shorten the width). Thanks in advance!

Comment
Add comment · Show 2
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 DayyanSisson · Aug 16, 2011 at 09:05 PM 0
Share

Also, if you can tell me how to show a percentage of health, that would help.

avatar image DayyanSisson · Aug 23, 2011 at 02:25 AM 0
Share

You don't have to use the Health Bar script exactly to answer this question. The Damage Reciever script has to stay the same though.

5 Replies

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

Answer by YikYikHeiHei · Aug 13, 2011 at 02:32 AM

It is add in to the Damage Receiver Script.

 function OnGUI()
 {
     GUI.HorizontalScrollbar(Rect (0,40,200,20), 0, hitPoints,0, 100);
 }
Comment
Add comment · Show 2 · 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 DayyanSisson · Aug 27, 2011 at 05:31 PM 0
Share

I forgot about this answer. This one works. Thanks!

avatar image GluedBrain · Sep 08, 2014 at 06:04 AM 0
Share

$$anonymous$$aking a health bar is ridiculously simple in the all new Unity UI. You just need to use a slider to create one and a bit of code. Check this article out Health Bar Unity 4.6

avatar image
1

Answer by Julien-Lynge · Aug 12, 2011 at 08:56 PM

There are seriously tons of posts on this, with examples for rectangular health bars, circular health bars, anything you could imagine.

Here's the top Google result for 'Unity Health Bar':

http://answers.unity3d.com/questions/17255/how-do-i-make-a-health-bar.html

Next time, check around at least a little bit before posting a question, because otherwise folks like me get grumpy.

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 DayyanSisson · Aug 12, 2011 at 09:35 PM 0
Share

I know there are other tutorials and other questions like this, but I need to use that specific damage receiver because other scripts depend on that damage receiver so I need help for THIS receiver. Otherwise I would never had asked this question (And I'm with you, I can't stand when people ask questions that have already been answered).

avatar image Julien-Lynge · Aug 12, 2011 at 09:39 PM 0
Share

okay, then I'm not understanding your question. What specifically are you trying to do? Is your issue with trying to access variables or methods in one script from another script?

avatar image DayyanSisson · Aug 13, 2011 at 06:57 PM 0
Share

yeah, that's my issue, I don't know how to link the two scripts so the health bar goes down.

avatar image
1

Answer by Statement · Aug 12, 2011 at 10:59 PM

Not sure if you were only looking to narrow the width...

 var scale = 1.0f;
 
 function OnGUI() {
     GUI.backgroundColor = Color.green;
     GUI.Button(Rect(10, 640, 510 * scale, 20), "Health");
 }

 // Call this to update the health.
 // Note: 100.0f should be max.
 function SetHealth(health : float) {
     scale = Mathf.Clamp01(health / 100.0f);
 }
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 DayyanSisson · Aug 23, 2011 at 02:03 AM 0
Share

I've tried everything, so I'm co$$anonymous$$g back to you. You're right, I'm only trying to narrow the width, and it has to be connected to that damage receiver. Right now, I have one huge script for the player, so the GUI Health Bar will be part of the Health Script. How do I use your script, to make the Health Bar get smaller as the health bar goes down?

avatar image
1

Answer by Julien-Lynge · Aug 13, 2011 at 07:56 PM

yeah, that's my issue, I don't know how to link the two scripts so the health bar goes down.

Alrighty, that's a very reasonable question to ask.

There are a number of ways to link scripts. If you want to do it by hand, create a public variable of type [name of script] and then drop your other script into it in the editor. You can then access public functions in the referenced script like so

otherCode.doSomething();

where otherCode is your reference to the other script and doSomething is your public function.

You can also look into GameObject.Find to get a reference to the game object containing the other function, and then GetComponent to access the script. There are also more advanced things like FindObjectOfType.

Finally, if you just want to send a message to a script in a game object without worrying about what the receiving script is called, you can use SendMessage. Don't know how to use SendMessage, and the script reference is confusing? There are quite a few notes on it here and in the forums.

Best of luck,

Julien

Comment
Add comment · Show 2 · 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 DayyanSisson · Aug 14, 2011 at 06:21 AM 0
Share

I'm learning. Thank you for showing me how to link scripts but now that scripts are linked, how do I make the health bar decrease for damage?

avatar image Julien-Lynge · Aug 14, 2011 at 06:34 AM 0
Share

I started replying to your message, and then something occurred to me: there's an even easier solution I hadn't thought of. You can specify the width of the health bar as a global (static) variable (see http://unity3d.com/support/documentation/ScriptReference/index.$$anonymous$$ember_Variables_26_Global_Variables.html). Then, to set it, all you need to do is call the name of the script and the variable like so:

$$anonymous$$yScriptName.healthBarWidth = [value]

You don't need any reference to the game object or the script component itself - by setting it static, we're telling Unity that we're only going to have one copy of that variable anywhere in the scene. From there, you use Statement's answer he gave below:

function OnGUI() { GUI.backgroundColor = Color.green; GUI.Button(Rect(10, 640, 510 * scale, 20), "Health"); }

where scale is our global/static var.

avatar image
0

Answer by McCerdo · Aug 07, 2014 at 10:40 AM

Here is the solution :

 var Health = 100;
 var position = Rect (500,15,200,20);
 
 function OnTriggerEnter (Objecte : Collider) {
     if (Objecte.tag == "Obstacle") 
     {
         Health -= 50;
         
     }
 }
 
 function Update ()
 {
     if (Health <= 0)
     {
         Application.LoadLevel (Application.loadedLevel);
     }
 }
 
  function OnGUI()
 {
     GUI.backgroundColor = Color.green;
     GUI.HorizontalScrollbar(position, 0, Health,0, 100);
 }

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 Graham-Dunnett ♦♦ · Aug 07, 2014 at 10:41 AM 0
Share

I updated your answer to make the code readable

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

7 People are following this question.

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

Related Questions

How to prevent guitexture from vertically shrinking? 0 Answers

Weapon Mesh Damage 1 Answer

How to get Enemy to break wall between it and Player. 1 Answer

Player Health Damage 2 Answers

This collision Enemy health code suddenly stopped 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