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 /
  • Help Room /
avatar image
0
Question by SpiderSensei · Sep 08, 2020 at 12:49 PM · simplenull referencehealth bar

Health Bar image doesn't scale down when Player takes damage

This is my code for my Player. When I press U, I can see in the variable window that the player is taking damage, but the Health Bar doesn't scale down.

Edit: I've found that health, when referenced by healthBar.SetHealth and healthBar.SetMaxHealth, is a null value? What am I missing here?

public class Player : MonoBehaviour {

 //variables
 //verify that public floats can be updated through stat progression
 public float maxHealth = 100;
 public float maxStamina = 100;
 public float maxMana = 100;
 public float healthIncreaseRate, staminaIncreaseRate, manaIncreaseRate;
 public float health, stamina, mana;

 public HealthBar healthBar;
 public StaminaBar staminaBar;
 public ManaBar manaBar;

 private bool dead = false;

 //???
 private bool fullHealth = true;
 private bool fullStamina = true;
 private bool fullMana = true;

 // Start is called before the first frame update
 public void Start()
 {
     health = maxHealth;
     stamina = maxStamina;
     mana = maxMana;
     healthBar.SetMaxHealth(maxHealth);
     staminaBar.SetMaxStamina(maxStamina);
     manaBar.SetMaxMana(maxMana);
 }

 // Update is called once per frame
 public void Update()
 {

     // if the player takes damage, health goes down
     if (Input.GetKeyDown(KeyCode.U))
     {
         TakeDamage(20);
     }

     // Possible deaths
     if (health <= 0)
         Die();

     //health, stamina, and mana increase over time
     if (health < maxHealth)
     {
         health += healthIncreaseRate * Time.deltaTime;
     }
     if (stamina < maxStamina)
     {
         stamina += staminaIncreaseRate * Time.deltaTime;
     }
     if (mana < maxMana)
     {
         mana += manaIncreaseRate * Time.deltaTime;
     }

 }

 public void Die()
 {
     print("You have died.");
     dead = true;
 }

 public void TakeDamage(float damage)
 {
     health -= damage;
     healthBar.SetHealth(health);
 }

}

The HealthBar script is pretty basic: public class HealthBar : MonoBehaviour {

 public Slider sliderHealth;

 public void SetMaxHealth(float health)
 {
     sliderHealth.maxValue = health;
     sliderHealth.value = health;
 }

 public void SetHealth(float health)
 {
     sliderHealth.value = health;
 }

}

Comment
Add comment · Show 19
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 streeetwalker · Sep 08, 2020 at 05:36 PM 1
Share

you're not getting any errors?

avatar image SpiderSensei streeetwalker · Sep 09, 2020 at 05:02 AM 0
Share

I'm not getting any errors or warnings. It's like I incorporated the HealthBar script incorrectly into my Player script, but I can't find anything wrong with my code. The sliders simply don't respond to anything that happens in the player script.

avatar image streeetwalker SpiderSensei · Sep 09, 2020 at 06:31 AM 1
Share

yeah, I don't see anything obvious in the script's reference to healthBar either.

If you put a debugger into healthBar.SetHealth, can you verify that the method is actually receiving the value you think you are sending?

Show more comments

2 Replies

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

Answer by streeetwalker · Sep 16, 2020 at 08:26 AM

You're right, you're missing a tiny thing. It's a mistake we all make often!

At the top of HealthBar.cs you have defined a Slider variable

  public Slider sliderHealth;



Now click on the object in the hierarchy that HealthBar.cs is attached to.

In the script component HealthBar on that object, you will see a field named 'Slider Health' - I bet is says "none."

Make sure you drag the slider object in the Canvas hierarchy to that field.

Comment
Add comment · 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
0

Answer by judahcline · Sep 10, 2020 at 01:24 PM

 if (Input.GetKeyDown(KeyCode.U)) 
 {
     TakeDamage(20);
 }

needs to be:

 if (Input.GetKeyDown(KeyCode.U)) 
 {
     TakeDamage(20f);
 }

a float will take a number as 0 if there is not an f behind it

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 streeetwalker · Sep 10, 2020 at 03:44 PM 0
Share

That's not true - the int will be cast as a float. Try it yourself.

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

210 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 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 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 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 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 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 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 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 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 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 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 avatar image

Related Questions

Anim object with Relative Position 1 Answer

How do I get my game to generate a random number for each counter,how do I make my game generate 2 different numbers at the same time 1 Answer

Can I assign no value to a variable like Gamemaker? 1 Answer

Convert int to binary 1 Answer

Help with a health bar 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