Increasing multiplier

Hello there, I was trying to create a multiplier that increase, let’s say, of 1 unit after every 7 seconds. I written this code, but it doesn’t work. Any suggestion?
Thanks in advance.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class scoreHandelr : MonoBehaviour
{
    [SerializeField] private TMP_Text scoreText;
    private float score;
    private int multiplier=1;
    private float timer = 0f;
    void Update()
    {
        timer +=Time.deltaTime;
        if(timer%7==0)
        {
            multiplier +=1;
        }
        score +=Time.deltaTime*multiplier;
        scoreText.text= Mathf.FloorToInt(score).ToString()+"\t"+"X"+multiplier.ToString();
    }
}

It doesn’t work because timer is a float and the modulus of a float will almost never be 0. A simple fix is to just check if the timer is greater or equal to 7, and then also reset the timer

timer += Time.deltaTime;
if (timer >= 7f)
{
    multiplier++;
    timer -= 7f;
}

We subtract 7 here - instead of setting timer to 0 - to account for the overflow decimals that may be around. So, 7.123 will become 0.123 and we still have that time already accounted for

3 Likes

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms