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
1
Question by Rich Coy · Apr 01, 2011 at 02:37 PM · javascripthealthbar

Healthbar Not Changing Size

Hello,

I'm using the script below for my player health bar. It's loosely based off of the popular "hack and slash" healthbar tutorial except I tried to rewrite it in javascript and I set the maximum width of the bar to 300, not half of the screen.

It's attached to my player character and it displays okay in the game.

When I go into the inspector and modify curHealth the displaying text in the bar changes but the size of the bar does not, however the limiting of the values between 0 and 100 is not functioning.

It also seems that passing a number to the AdjustCurrentHealth function has no effect. These are probably because of coding errors on my part when I tried to rewrite the script in js.

Thanks for any help you can offer.

var maxHealth : int = 100; var curHealth : int =100; var maxBarLength : int = 300; var healthBarLength;

function Start () { AdjustCurrentHealth(curHealth); }

function OnGUI() {

 GUI.Box(new Rect(10,10, healthBarLength, 20), curHealth + "/" + maxHealth);

}

function Update() {

}

function AdjustCurrentHealth(adj : int){ curHealth += adj;

     if(curHealth < 0) {
         curHealth = 0;
     }
     if(curHealth > maxHealth) {
         curHealth = maxHealth;
     }
 healthBarLength = maxBarLength * (curHealth / maxHealth);
 }

In The collision script I'm calling the AdjustCurrentHealth this way:

var playerHealthInstance : PlayerHealth; static var GEAR_COUNT = 0;

function Start() { playerHealthInstance = GameObject.Find("Player").GetComponent(PlayerHealth); }

function OnTriggerEnter (other : Collider) { if(other.gameObject.CompareTag("Gear")) { // Add Gear To Inventory GEAR_COUNT += 1; playerHealthInstance.AdjustCurrentHealth(-10); print("You've Collected " + GEAR_COUNT + " Gears!");

     // Destroy The Gear
     Destroy(other.gameObject);
 }
 else if(other.gameObject.CompareTag ("Battery"))
 {
     // Add Battery To Player's Life
     playerHealthInstance.AdjustCurrentHealth(10);
     print("You've Collected A Battery");

     // Destroy The Battery
     Destroy(other.gameObject);
 }

}

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 e-bonneville · Apr 01, 2011 at 02:46 PM

I see one thing that could be the problem. You're declaring the integer variable adj before passing it into the function. Due to JS's automatic inference, it's declared as 0 (or possibly null, don't remember which), because you're not assigning a value to it in the header. To fix this, simply remove adj's declaration and the script should work as expected.

Also, I don't remember if JS's automatic inference applies to function declarations, so you'll also want to edit function AdjustCurrentHealth(adj){ to function AdjustCurrentHealth(adj : int){ just to be sure.


EDIT: Because you're only calling AdjustCurrentHealth once (in the Start function) it does not update during the game, so modifying the variables in the Inspector would change them, but this won't be reflected in your health bar (because it's not getting updated).

What you need to do is move the call to AdjustCurrentHealth to the Update() function for now. Later in development, you can move it to the enemies and whatever other damage-causing objects you have in the environment

Comment
Add comment · Show 14 · 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 Rich Coy · Apr 01, 2011 at 02:59 PM 0
Share

I've adjusted the two items you mentioned unfortunately they did not solve any of the issues. I've noticed that I'm declaring curHealth to be 60 but the bar still comes up at 100. I've edited the code above to the new version.

avatar image e-bonneville · Apr 01, 2011 at 03:02 PM 0
Share

Really? Thats strange, I'll take another look at it. :/

avatar image e-bonneville · Apr 01, 2011 at 03:06 PM 0
Share

What are the variables declared to be in the Inspector? That overrides the variable declarations on the script. As I mentioned in my previous answer, a script instance is different from the actual script, and thus, the variables on that particular instance could (and often do) differ from the variables you declare in the script. Still looking at the script, but I don't see anything wrong with it off the top of my head.

avatar image e-bonneville · Apr 01, 2011 at 03:13 PM 0
Share

Oh, got it. Because you're only calling AdjustCurrentHealth once (in the Start function) it does not update during the game, so modifying the variables in the Inspector would change them, but this won't be reflected in your health bar (because it's not getting updated). What you need to do is move the AdjustCurrentHealth to the Update() function for now. Later in development, you can move it to the enemies and whatever other damage-causing objects you have in the environment

avatar image Rich Coy · Apr 01, 2011 at 05:11 PM 0
Share

Well moved things around and still no go. Frustrating. I've updated the code above to show the current state of the code and added the collision.js code where the function is called. The bar stays at 100 even after pickup add and subtract from the total.

Show more comments
avatar image
1
Best Answer

Answer by Parthon · Apr 01, 2011 at 10:45 PM

Firstly, remove adjust from Start() and change var healthBarLength; to var healthBarLength : int = maxBarLength;

Now are you sure that the gears are getting picked up? Add in a Debug.Log() to Adjust health so you can make sure it's being called. It would be a good idea to log adj and curHealth;

Also, instead of

playerHealthInstance = GameObject.Find("Player").GetComponent(PlayerHealth);

you want

playerHealthInstance = GameObject.Find("Player").GetComponent("PlayerHealth");

PlayerHealth in that first line should have been "PlayerHealth", that could be the issue.

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 Rich Coy · Apr 01, 2011 at 10:56 PM 0
Share

Thanks. I've made the modifications you suggested but I get an "Unexpected token: PlayerHealth" error on the Collisions.js script.

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

No one has followed this question yet.

Related Questions

GUI Texture change width script. javascript 0 Answers

Healthbar in js 0 Answers

How Do I Make A Health Bar 6 Answers

Health Bar 0 Answers

Setting Scroll View Width GUILayout 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