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;
}
}
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.
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.
What do you mean pointer up and pointer down event triggers on the button? Can you post an image?
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;
}
}
Works great. Thank you very much. Any chance I could get a brief explanation of what I was doing wrong?
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.
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.
Your answer
