Trouble with Incremental Score Code per second.

The following code below does not tick one point on the score board it ticks about 10 points. I want a smooth tick of one point per frame maybe. I just need consistency of the score adding up. It seems to be adding in chunk of 10 points or so. Any modifications tot eh below code to do this? Thanks for any help.

//this example increments the score by 1 every 1 second
public class ScoreTicker : MonoBehaviour
{
    //this is what our score should be like after we're done 'ticking'
    public int TargetScore;

    //this is the value representing our 'real' score at any given time
    public int CurrentScore;

    //this is how much we increment the score every tick - set in the inspector
    public int ScorePerTick = 1;

    //this is how long we wait between ticks - set in the inspector
    public float TickInterval = 1;

    void Start()
    {
        //in this case, as in most cases, I start the coroutine using it's name, so it can be stopped later
        StartCoroutine("Ticker");
    }

    //raise the score target
    public void AddScore(int score)
    {
        TargetScore += score;
    }

    //this will increment the CurrentScore towards the TargetScore over time
    public IEnumerator Ticker()
    {
        //this loop will run forever so you can just call AddScore and the ticker will continue automatically
        while (true)
        {
            //we don't want to increment CurrentScore to infinity, so we only do it if it's lower than TargetScore
            if (CurrentScore < TargetScore)
            {
                CurrentScore += ScorePerTick;

                //this is a 'safety net' to ensure we never exceed our TargetScore
                if (CurrentScore > TargetScore)
                {
                    CurrentScore = TargetScore;
                }
            }

            //wait for some time before incrementing again
            yield return new WaitForSeconds(TickInterval);
        }
    }
}

I think you should use InvokeRepeating instead or do it all in Update() with a 1 second delay between ticks.

you can make a coroutine call itself. some example code

float interval;

void Start(){
    StartCoroutine("Ticker", interval);
}
IEnumerator Ticker(float t){
    yield return new WaitForSeconds(t);
    //add or subtract one second from time here
    StartCoroutine("Ticker", interval);
}
1 Like

The code you have does not indicate a problem, so the problem must be happening elsewhere. You don’t have to use a Coroutine for a simple thing like “wait x amount of time”.

    //this is what our score should be like after we're done 'ticking'
    public int targetScore;

    //this is the value representing our 'real' score at any given time
    private int currentScore;

    //this is how much we increment the score every tick - set in the inspector
    public int scorePerTick = 1;

    //this is how long we wait between ticks - set in the inspector
    public float tickInterval = 1f;

    // A timer based on Time.timeSinceLevelLoad + tickInterval
    float nextTickTime;

    //raise the score target
    public void AddScore(int score)
    {
        targetScore += score;
    }

    void Update()
    {
        if (currentScore < targetScore)
        {
            IncrementScoreIfTickTimeIsUp();
        }
    }

    void IncrementScoreIfTickTimeIsUp()
    {
        if (Time.timeSinceLevelLoad > nextTickTime)
        {
            nextTickTime = Time.timeSinceLevelLoad + tickInterval;
            currentScore = Mathf.Min(currentScore + scorePerTick, targetScore);
        }
    }
1 Like