variable -= Time.deltaTime acts not as it should do?

I have two scripts, a singleton, global static MainManager and and a custom Button script. For whatever reason, as soon it starts counting down with time.deltatime, it jumps immediately to zero… I don’t understand why… it works if I multiply Time.deltaTime with something like .02f but it seems like a rather odd behaviour to me.

Mainmanager has public float cooldown = 2
the button script has this:

public class ActivateButtons : MonoBehaviour, IPointerDownHandler
{
    MainManager mm;
    float cooldownCache;

    [SerializeField] FieldType fieldType;

    private void Start()
    {
        mm = MainManager.Instance();
        mm.cooldown = 2;
        cooldownCache = mm.cooldown;
    }

    public void OnPointerDown(PointerEventData eventData)
    {
        mm.cooldown = cooldownCache;
        mm.countdownStart = true;
    }

    private void Update()
    {
        if (mm.countdownStart)
        {
            mm.cooldown -= Time.deltaTime;
        }

        if (mm.cooldown <= 0)
        {
            mm.countdownStart = false;
        }
    }

How do you know, how are you debugging? Use Debug.Log to find out exactly why https://discussions.unity.com/t/748729/14

I see the number in my inspector and no, I’m not good at debugging… but I sort of found the issue. I have multiple custom buttons with this script attached and then I click on all buttons at the same time, the timer starts to accelerate, which is the reason why multiplying by 0.2f works and why it jumps directly to zero, now I have to find a way to stop the timer from accelerating.

Don’t manage a cooldown in one class from some other class, let it manage itself. Or at the very least call a function so you can track down these issues more easily.

1 Like