• 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
0
Question by MounirDev · Jul 24, 2019 at 09:02 PM · unity 2dimagedecreasefill

image.fillamount doesnt decrease

hi, i'm using the fill amount to change the health bar state, the value of decreasing depends on the weapon, i store the damage value in a variable called "value" and from another script i do this :

 public void SetValue(float value)
     {
         maskblue.fillAmount -= value;
     }

the fill amount doesn't change and stay 1, but if i change " value" by a float number ( 1f, 0.5f ) it works ! notice that in the other script i call the previous function "SetValue" like this :

  public void ChangeHealth( float amount )
     {
         currentHealth += amount;
         HealthBar.instance.SetValue(amount);
     }

where the value of " amount" depends on the weapon's bullet ( -0.1 , -0.2 ...) i only did the -0.1 for no so.. I hope someone can help me! thank you.

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 I_Am_Err00r · Jul 24, 2019 at 09:10 PM

How are you referencing this health bar script in the weapon script? It doesn't look like the SetValue() function is getting the (float value) amount from that weapon script if it works when you manually enter the value.


An example would look something like this

 public class Damage : Monobehaviour
 {
 HealthBarClass healthBar;
 
 public void Start()
 {
 healthBar = GameObject.FindObjectOfType<HealthBarClass>();
 }
 
 public void Damage()
 {
 damage = weapon.Damage;
 healthBar.SetValue(damage);
 }
 }



Comment
Add comment · Show 2 · 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 MounirDev · Jul 24, 2019 at 09:29 PM 0
Share

@I_Am_Err00r i'll try to write exactly what i did : 1) in the BulletTrigger script, when the bullet triggers the Ship i call ChangeHealth() from ShipDamage script :

 void OnTriggerEnter2D(Collider2D other)
     {
      //usual code
                 ShipDamage ship = other.gameObject.GetComponent<ShipDamage>();
                 ship.ChangeHealth(-0.1f);
                 Destroy(gameObject);
     }

now in the ShipDamage script, the ChangeHealth() looks like this :

  public void ChangeHealth(float amount)
     {
         currentHealth += amount;
         HealthBar.instance.SetValue(amount);
     }

this will decrease currentHealth by 0.1f, this used to work so far. in a third script, HealthBar i have this :

 public void SetValue(float value)
     {
         maskblue.fillAmount -= value;
     }

the fill amount doesnt change at all!

avatar image MounirDev · Jul 24, 2019 at 11:05 PM 0
Share

i've found the problem ! "amount" is actually a negative value lool so i just have to write : maskblue.fillAmount += value;

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

112 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

Related Questions

How to change the end of image with type "Filled" 1 Answer

Image Fill Bug 0 Answers

Image custom fillOrigin 1 Answer

Image is normal in scene view but in game view it looks pixelated. 0 Answers

Fill an image with multiple colors? 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges