Unity UI Timer not working proprely

Hello, I’m trying to make a clock that counts time normaly in unity and the seconds are beeing wierd. But because this is a game i want the time to run faster, so I added a * 8 after Time.deltaTime. Maybe that is messing it up ? I realy don’t know.
anyway this is the code.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class TimeCycle : MonoBehaviour {

    [Header("Time")]
    [ShowOnlyAttribute]
    public float time;
    [Space(7)]
    [Header("Time of day")]
    public float morningTime;
    public float dayTime;
    public float nightTime;
    [Space(7)]
    [Header("Time Bool")]
    public bool isMorning;
    public bool isDay;
    public bool isNight;
    [Space(7)]
    [Header("Clock UI")]
    public Text clockText;

    private float minutes;
    private float seconds;

    void Start(){
        isNight = true;
    }

    void Update(){
        time += Time.deltaTime;

        if (time > morningTime && time > morningTime + 0.001f ){
            isMorning = true;
            isDay = false;
            isNight = false;
        }if (time > dayTime && time > dayTime + 0.001f){
            isDay = true;
            isNight = false;
            isMorning = false;
        }if (time > nightTime && time > nightTime + 0.001f){
            isNight = true;
            isDay = false;
            isMorning = false;
        }

        if (time > nightTime + 0.001f) {
            time = 0;
        }

        minutes = time / 60;
        seconds = time % 60;

        clockText.text = minutes.ToString("F0") + (":") + seconds.ToString("F0");
    }
}

The seconds count up to 30 then the minutes change to 1, the seconds contuniue to 60 the got back to 0 and finaly when they reach 30 again the minutes change to 2.
Thankyou for helping.

2977548--221361--giphy.gif

I believe what’s happening is that your minutes is being rounded to the nearest whole number when it’s being displayed. Try setting minutes to Mathf.Floor(time / 60).

Bonus 1: rather than constructing strings like that, it’s often more readable (and MUCH better in terms of performance) to use string.Format.

clockText.text = string.Format("{0:F0}:{1:F0}", minutes, seconds);

strong.Format replaces anything in {brackets} with the .ToString value of the objects you pass as the additional parameters. {0} gets replaced with the first parameter, {1} with the second parameter, etc. And adding stuff behind a colon like {0:F0} is equivalent to .ToString(“F0”).

Bonus 2: Your day/night bools are being set weirdly. Here, try something like this with them:

isMorning = (time > morningTime && time < dayTime);

13 lines of code become 3 lines

That was it. Thank you so much for the solution and the two bonuses. That helped a lot :slight_smile: