#button working different after pressed... please help
1 messages · Page 1 of 1 (latest)
Show the code of the button click handler
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class FillButtonPressed : MonoBehaviour, IPointerUpHandler, IPointerDownHandler {
private bool pressed = false;
private void Update() {
if(pressed) {
transform.parent.GetComponentInChildren<DrinkBar>().Add(1);
}
}
public void OnPointerDown(PointerEventData data) {
pressed = true;
}
public void OnPointerUp(PointerEventData data) {
pressed = false;
}
}
and the fill bar code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class DrinkBar : MonoBehaviour {
public int maximum;
public int current;
public Image mask;
private void Start() {
}
private void Update() {
GetCurrentFill();
if(current > maximum) current = maximum;
}
private void GetCurrentFill() {
float fillAmount = (float)current / (float)maximum;
mask.fillAmount = fillAmount;
}
public void Add(int amount) {
current += amount;
}
public void Empty() {
current = 0;
}
}
i mean it should work the same after letting go and pressing again...?
I would say the error comes from GetCurrentFill, that sets the fill amount before checking if current > maximum
nope, still not working
Debug it is, then
In both OnPointer* methods, and in the Add(int) method
Make sure that they're getting called as expected.
Ie. One log per OnPointer* per click/release, two or more will indicate duplicate scripts
huh? it works now, but i didnt change anything..?
We'll blame it on unsaved scripts, or cursed project then
wait
now it dont work?
what?
ok so it doesent work
my fault
but it isnt duplicated scripts...
its filling smooth on first press, after that it just "lags"
Okay, so try moving the whole code that's in DrinkBar:Update inside of DrinkBar:Add
By lag you mean it's adding inconsistently?
It should be doing the same thing as Add is called each frame when holding the click down
Works fine for the first time, adds inconsistently (not smoothly) afterwards
really strange
thats not working either
Okay, so now move the GetComponent call that calls Add in OnPointerDown, remove it from Update
k
See if clicking repeatedly on it increases it one by one
Removed from Update and added it in OnPointerDown, right?
And it's consistent.
So the problem comes from calling that in Update
Which adds 1 each frame, so 60 per second at 60fps
Before that, does the OnPointerUp log as expected? Never used them too much so I have no idea if they're consistent
yeah
Even if its that the problem, i find it weird that it increments inconsistently after the 2nd button press, it should have been the case for the 1st press too
yea thats the thing
Unless your fps is changing drastically after the 2nd button press somehow
Yep there's definitely some weirdness going on here
Try caching the result of your GetComponent call in Awake or Start, that will lighten up the load on that script
it isnt "inconsistent" after the second press, just not the same consistent as the first press
how would i do that
oh i see
Store it in a field, as it's seemingly constant, you're getting the same script over and over
still the same!?
Change Update to FixedUpdate in the button class so it would increment by 1 consistently regardless of fps