• 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
1
Question by johnniks · Nov 13, 2015 at 10:01 PM · healthbarcolor.lerp

healthbar color.lerp issue

I'm trying to make my healthbar turn color the more damage it gets (green -> yellow -> red), but for some reason the healthbar vanishes when my enemies are being hit :/ The code works fine, beside the coloring stuff in void TakeDamage(float amt). If I comment out the coloring part of the code, then my healthbar works without issues.

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class EnemyHealth : MonoBehaviour {
     
     public float hitPoints = 100f;
     public float currentHitPoints;
     public GameObject destroyFX;
     public Image healthbar;
 
     public Color32 startColor;
     public Color32 middleColor;
     public Color32 endColor;
 
     void Start () {
         currentHitPoints = hitPoints;
     }
     
     void TakeDamage(float amt) {
         currentHitPoints -= amt;
 
         
         float healthCalc = currentHitPoints / hitPoints; 
         setHealthbar(healthCalc);
 
         if (healthCalc < 0.1f) {
             healthbar.color = Color.Lerp(endColor, middleColor, healthCalc * 2);
         }
         else {
             healthbar.color = Color.Lerp(middleColor, startColor, (healthCalc - 0.5f) * 2);
         }
 
         if (currentHitPoints <=0) {
             currentHitPoints = 0;
             Die();
         }
     }
     
     void Die() {
         if(gameObject.tag == "Enemy") {
             Instantiate(destroyFX, this.transform.position, this.transform.rotation); 
             Destroy (gameObject);
         }
     }
 
     public void setHealthbar (float enemyHealth) {
         healthbar.transform.localScale = new Vector3(enemyHealth, healthbar.transform.localScale.y, healthbar.transform.localScale.z);
     }
 
 }
 
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by joemane22 · Nov 14, 2015 at 06:38 AM

The only problem I see is in

 healthbar.color = Color.Lerp(middleColor, startColor, (healthCalc - 0.5f) * 2);

If healthCalc is less than 0.5 than the result will be negative and won't give you the desired results. Instead do

 healthbar.color = Color.Lerp(middleColor, startColor, Mathf.Clamp01(healthCalc - 0.5f) * 2);

That way the result will either be zero or positive. Hope that fixes your issue.

Comment
Add comment · Show 7 · 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 johnniks · Nov 14, 2015 at 09:50 PM 0
Share

That didn't work :( the healthbar still vanished into thin air whenever the gameobject is hit... but the healthbar works fine without the color code >_>

avatar image joemane22 johnniks · Nov 15, 2015 at 09:15 PM 0
Share

Well, you will have to do some debugging. Run your game and when the healthbar disappears pause the game. Right click on the inspector tab at the top of the inspector and change it to debug view so you can see all the variables. Check the scale variables, the color and so on. $$anonymous$$ake sure you check alpha levels on the colors as well. At least find what the culprit is so you can focus on seeing what went wrong.

avatar image johnniks joemane22 · Nov 15, 2015 at 09:30 PM 0
Share

I'm guessting the issue is that I need to put my float healthCalc and my if-statement (healthCalc < 0.1f) {...} down into the public void setHealthbar (and the color code). But I don't know how to change setHealth (float enemyHealth) {...} into something where I don't need the float enemyHealth, or else I won't be able to call it with setHealth(); in void TakeDamage, because it shouldn't have a set value e.g. setHealth(0.0f); but I'm not a great programmer, so I really don't know how to go about doing that :(

Show more comments
Show more comments

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

34 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

Related Questions

Healthbar color lerp between green, yellow, and red 4 Answers

How do I make my healthbar change color depending on damage? 1 Answer

Can i make a game by Unity from different assets? 1 Answer

Help with Zelda type health bar! 0 Answers

How do I create an undertale player health bar in C#? 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