Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 CezaryDevelopment · Nov 21, 2015 at 03:57 PM · variableupdategetcomponenthealthbarhealth

My health bars image.fillAmount doesnt change.

Im making a health bar and in the update function I set the image.fillAmount to be a variable I created. For some reason the health bar stays at 100 %. Can you tell me please why this happens please! Health_bar script: using UnityEngine; using UnityEngine.UI; using System.Collections;

 public class Health_bar : MonoBehaviour
 {
     public GameObject Enemy;
 
 
     // Update is called once per frame
     void FixedUpdate()
     {
         float EnemyHealth = Enemy.GetComponent<Enemyscripts>().Health;
         Image image = GetComponent<Image>();
         image.fillAmount = (EnemyHealth);
         Debug.Log("Health : " + EnemyHealth);
     }
 }

By the way the GetComponent().Health is a variable in another script. Also the log is displayed with updated health every time health is lost. I tried Update() function but it didnt work either.

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

4 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by justaddice83 · Feb 28, 2020 at 10:19 AM

Image Type has to be "Filled", otherwise the fillAmount will be ignored and always be considered = 1.

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 CatStrategist · Nov 21, 2015 at 06:33 PM

image.fillAmout takes arguement between 0 and 1. I bet your EnemyHealth is value between 0 and 100 so you need to divide it by one hundred:

 image.fillAmount = (EnemyHealth)/100;
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 Nathan1_ · Nov 21, 2015 at 06:34 PM

The fillAmount needs to be a value between 0 and 1, are you setting it using a percent? 50 for example would always be above 1.

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 CezaryDevelopment · Nov 21, 2015 at 05:45 PM

I fixed the problem my self. I forgot to divide my EnemyHealth by 100. public class Health_bar : MonoBehaviour { public GameObject Enemy;

      // Update is called once per frame
      void FixedUpdate()
      {
          float EnemyHealth = Enemy.GetComponent<Enemyscripts>().Health;
          Image image = GetComponent<Image>();
          image.fillAmount = (EnemyHealth/100);
          Debug.Log("Health : " + EnemyHealth);
      }
  }
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 Domo23000 · Nov 21, 2015 at 06:18 PM 0
Share

Yes this is because the fill amount goes from 0 to 1 not 0 to whatever health you have

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

35 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

Related Questions

How to make GUI variables compatible with the UI system? 0 Answers

Inputting another scripts variable as a float 1 Answer

Can't update a public Variable in another script.. 1 Answer

How to sum up values from clones 1 Answer

double output even though it should be possible 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