#button working different after pressed... please help

1 messages · Page 1 of 1 (latest)

daring dune
#

i have a button to add more to a fill image (progress bar). after i hold the button it progresses just fine, with one add every update(). but when i have pressed it, let go, and presses it again, it increments the bar with 10 steps (???). why does it do that?

dense pulsar
#

Show the code of the button click handler

daring dune
#
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...?

dense pulsar
#

I would say the error comes from GetCurrentFill, that sets the fill amount before checking if current > maximum

daring dune
#

so i should set it before?

#

or inside the function

dense pulsar
#

Check before setting, yes

#

(Swap the statements in Update)

daring dune
#

nope, still not working

dense pulsar
#

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

daring dune
#

huh? it works now, but i didnt change anything..?

dense pulsar
#

We'll blame it on unsaved scripts, or cursed project then

daring dune
#

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"

dense pulsar
#

Okay, so try moving the whole code that's in DrinkBar:Update inside of DrinkBar:Add

fresh glacier
#

By lag you mean it's adding inconsistently?

dense pulsar
#

It should be doing the same thing as Add is called each frame when holding the click down

dense pulsar
daring dune
#

really strange

dense pulsar
#

Okay, so now move the GetComponent call that calls Add in OnPointerDown, remove it from Update

daring dune
#

k

dense pulsar
#

See if clicking repeatedly on it increases it one by one

daring dune
#

no it doesnt

#

i removed the add call

dense pulsar
#

Removed from Update and added it in OnPointerDown, right?

daring dune
#

oh

#

sec

#

yes, it increments per click now

dense pulsar
#

And it's consistent.
So the problem comes from calling that in Update

#

Which adds 1 each frame, so 60 per second at 60fps

daring dune
#

so like Time.deltaTime?

#

but it works first time without that...

dense pulsar
#

Before that, does the OnPointerUp log as expected? Never used them too much so I have no idea if they're consistent

daring dune
#

yeah

fresh glacier
daring dune
#

yea thats the thing

fresh glacier
#

Unless your fps is changing drastically after the 2nd button press somehow

dense pulsar
#

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

daring dune
dense pulsar
#

Store it in a field, as it's seemingly constant, you're getting the same script over and over

daring dune
#

still the same!?

fresh glacier
#

Change Update to FixedUpdate in the button class so it would increment by 1 consistently regardless of fps