- Home /
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);
}
}
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
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.
Really? Thats strange, I'll take another look at it. :/
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.
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
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.
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.
Thanks. I've made the modifications you suggested but I get an "Unexpected token: PlayerHealth" error on the Collisions.js script.
Your answer
Follow this Question
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