Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 seannorbury · Nov 09, 2016 at 02:54 AM · c#imagepowerbar

Can I use image.fillAmount to fill a power bar with a float I have fluctuating between 0 and 100?

I am able to get the power bar to fill up at a fixed time using the commented out part of the code, but I would like to have the bar go up and down according to the percentage float. Here is the code:

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class PowerMeter : MonoBehaviour
 {
     public Transform PowerMeterBar;   
     public bool buttonPressed = false; 
        
 
     public int sign = 1;
 
     [SerializeField]
     private float percentage;
     [SerializeField]
     private float currentAmount;
     [SerializeField]
     private float speed = 30;
     
     void Start ()
     {
         
     }
 
     public void ButtonDown()
     {
         buttonPressed = true;
     }
 
     public void ButtonUp()
     {
         buttonPressed = false;
     }
 
     public void Update()
     {
         if (buttonPressed)
         {
             percentage += sign;
             Debug.Log(percentage);
 
             if (percentage == 100 || percentage == 0)
                 sign *= -1;                        
         }
 
         //if (currentAmount < 100)
         //{
         //    currentAmount += speed * Time.deltaTime;
         //}
         //PowerMeterBar.GetComponent<Image>().fillAmount = currentAmount / 100;
 
         PowerMeterBar.GetComponent<Image>().fillAmount = percentage / 100;
     }
 
 
 }

Comment
Add comment · Show 4
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 TBruce · Nov 09, 2016 at 10:29 PM 0
Share

Are you still looking for an answer to this question? If so, from what I understand, you want an image to fill (and unfill itself) as long as the mouse button is pressed (does it matter which one). And if the button is released filling/unfilling will stop until the mouse button has been pressed again.

avatar image seannorbury TBruce · Nov 09, 2016 at 11:29 PM 0
Share

Yes, except I am using pointer up and pointer down event triggers on the button. I am able to fill the image using: currentAmount += speed Time.deltaTime, and Power$$anonymous$$eterBar.GetComponent().fillAmount = currentAmount / 100, but I cannot get it to work using percentage ins$$anonymous$$d of currentAmount.

avatar image TBruce seannorbury · Nov 09, 2016 at 11:36 PM 0
Share

What do you mean pointer up and pointer down event triggers on the button? Can you post an image?

Show more comments

1 Reply

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

Answer by TBruce · Nov 10, 2016 at 12:14 AM

This will work - tested

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class PowerMeter : MonoBehaviour
 {
     public Image powerMeterBar;   
     public bool buttonPressed = false; 
 
     public int sign = 1;
 
     [SerializeField]
     private float percentage;
     [SerializeField]
     private float currentAmount;
     [SerializeField]
     private float speed = 30;
     
     void Start ()
     {
         if (powerMeterBar != null)
         {
             percentage = powerMeterBar.fillAmount * 100;
         }
     }
 
     public void ButtonDown()
     {
         buttonPressed = true;
     }
 
     public void ButtonUp()
     {
         buttonPressed = false;
     }
 
     public void Update()
     {
         if (buttonPressed)
         {
             percentage += sign;
             Debug.Log(percentage);
 
             if (percentage >= 100 || percentage <= 0)
             {
                 sign *= -1;
                 percentage = ((percentage <= 0) ? 0 : 100);
             }
         }
         powerMeterBar.fillAmount = percentage / 100;
     }
 }
Comment
Add comment · Show 4 · 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 seannorbury · Nov 10, 2016 at 12:31 AM 0
Share

Works great. Thank you very much. Any chance I could get a brief explanation of what I was doing wrong?

avatar image TBruce seannorbury · Nov 10, 2016 at 12:48 AM 0
Share

First thing I did was change this

 public Transform Power$$anonymous$$eterBar;

to this

 public Image power$$anonymous$$eterBar;

because you only need access to the image not the transform and also it was no good that you continually did this every frame

 Power$$anonymous$$eterBar.GetComponent<Image>().fillAmount = percentage / 100;

Second thing I did was to sync percentage to the power$$anonymous$$eterBar.fillAmount by doing this at game start

 percentage = power$$anonymous$$eterBar.fillAmount * 100;

Everything else was clean up. Even this

 percentage = ((percentage <= 0) ? 0 : 100);

is not needed, you can remove it if you want.

avatar image seannorbury TBruce · Nov 10, 2016 at 02:00 AM 0
Share

Thank you for the explanation. It is throwing a NullReferenceException object reference not set to an instance of an object on line 50, but it works just fine.

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.

Follow this Question

Answers Answers and Comments

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

adjust image size with mouse 0 Answers

Image Colour change not working with anythign other than base preset colours 2 Answers

How do I display an image from the script ? 0 Answers

Hotbar appears when a certain gameobject is selected? 1 Answer

Changing the image of a button with a script 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