Clicker Game - Help Making Score Count Up Within 1 Second

Hey all! I am working on a clicker game and am implementing an auto click upgrade. Right now the code below has it set up that after I make the first purchase, every second I am “paid” points equivalent to the upgrade tier I am at. 1 upgrade = 1 point a second, 2 upgrade = 2 points a second, 3 = 3, etc.

public void autoClick()
    {
        InvokeRepeating("Payout", 1f, 1f);
    }

    public void Payout()
    {
        gm.Money = gm.Money + (upgradeTier -1);
    }

This all works fine, expect for the fact that because of Invoke, I am receive the points in 1 lump sum ever second. If i am at upgrade tier 2, it looks as though the score is counting up by 2 points. My goal is to set up a system in which the score counts or ticks up to pay out the correct amount every 1 second, but ticks up 1 point at a time. If it needs to pay 2 it will tick up by 1 point every 0.5f, if it is by 3 it will tick up every 0.33f etc.

I tried something like this below but it only results in being paid out 1 time, and never again.

public void autoClick()
    {
 InvokeRepeating("Payout", 0f, (1 / (upgradeTier - 1)));  
    }

    public void Payout()
    {
        gm.Money = gm.Money + 1;
    }

Advice would be helpful!

A side note is that it is set up that when the upgrade is purchased the first time it called autoClick() only once. Every other purchase or upgrade should not be calling autoClick() again.

InvokeRepeating, which I don’t really use often, I’m pretty sure can’t be modified once called. Changing the value of upgradeTier after calling InvokeRepeating shouldn’t change the timing. I believe you’d have to cancel the previous InvokeRepeating and then call it again to restart the timer at different values. Note this may require verifying, but I feel that it should work that way. You’re most likely going to want to use something else for your timing.

However, note that InvokeRepeating starts to loose accuracy after a bit. So you might need to determine if it’s the right timer to use in the long run. Someone running an idle game for an hour or so may start to see the numbers be a bit off.

Another thing to note, your idea may also not be the best if your upgrade can go up really high. So keep in mind the number of popup “1” that need to appear at your highest level.

1 Like