Trying to get my lap timer to work...

I am working on a racing game project and are trying to get the final few details in palce such as UI elements. one of these things is a lap timer

I have been following Imphenzias Low Poly racing tutorials and tried to adapt the code to my own but for whatever god on earth reason, I just cant get the lap timer to work.

Here is the script on my end:

using JetBrains.Annotations;
using TMPro;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class TrackManagement : MonoBehaviour
{
    public float finalLap;
    public GameObject lapTrigger;
    public float currentLap;
    private float lapTimer;
    public float currentLapTimer = 0;
    public float bestLapTimer = Mathf.Infinity;
    public float lastLapTimer = 0;
    public GameObject finishedRaceCanvas;
    public GameObject RaceUI;
    public TMP_Text currentLapTime;
    public TMP_Text bestLapTime;
    public TMP_Text lastLapTime;
    public TMP_Text lapCount;




    void Start()
    {
        Cursor.visible = false;

       

        currentLap = 0; 
    }

    void Update()
    {
        lapTimer = Time.deltaTime;
        currentLapTimer = lapTimer > 0 ? Time.deltaTime - lapTimer : 0;
    }
    void OnTriggerEnter(Collider other)
    {
        if (other.gameObject == lapTrigger)
        {
           
            currentLap++;
            Debug.Log("New Lap Started!");

            lapCount.text = $"Current lap {currentLap} of {finalLap -1}";
            currentLapTime.text = $"Time {(int)currentLapTimer / 60}:{currentLapTimer % 60:00.000} ";
           



            if (currentLap == finalLap)
            {
                EndRace();
                Debug.Log("Your Winner!");
               
            }
        }
    }
    void EndRace()
    {
        Time.timeScale = 0;
        finishedRaceCanvas.SetActive(true);
        RaceUI.SetActive(false);
    }
}

Is the problem that I am using TextMeshPro instead of regualr text?

You haven’t yet explained what the problem actually is, so it’s difficult to say! :stuck_out_tongue:

I found the problem it was that I hadn’t put the lapTimer = Time.time in it’s own void. Got taht that working. I have also separated the the text elements into it’s own script.

The scripts now looks like this:

using JetBrains.Annotations;
using TMPro;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class TrackManagement : MonoBehaviour
{
    public float finalLap;
    public GameObject lapTrigger;
    public float currentLap;
    private float lapTimer;
    public float currentLapTimer { get; private set; } = 0;
    public float bestLapTimer { get; private set; } = Mathf.Infinity;
    public float lastLapTimer { get; private set; } = 0;
    public GameObject finishedRaceCanvas;
    public GameObject RaceUI;
    public TMP_Text lapCount;




    void Start()
    {
        Cursor.visible = false;

       

        currentLap = 0; 
    }

    void Update()
    {       
        currentLapTimer = lapTimer > 0 ? Time.time - lapTimer : 0;
    }
    void OnTriggerEnter(Collider other)
    {
        if (other.gameObject == lapTrigger)
        {
            StartLap();
            EndLap();

            if (currentLap == finalLap)
            {
                EndRace();
                Debug.Log("Your Winner!");
            }
        }
    }
    void StartLap()
    {
        currentLap++;
        Debug.Log("New Lap Started!");

        lapTimer = Time.time;

        lapCount.text = $"Current lap {currentLap} of {finalLap - 1}";
    }
    void EndLap()
    {
        lastLapTimer = Time.time - lapTimer;
        bestLapTimer = Mathf.Min(lastLapTimer, bestLapTimer);
    }
    void EndRace()
    {
        Time.timeScale = 0;
        finishedRaceCanvas.SetActive(true);
        RaceUI.SetActive(false);
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;

public class DisplayTIme : MonoBehaviour
{

    public TMP_Text currentLapTime;
    public TMP_Text bestLapTime;
    public TMP_Text lastLapTime;

    private float currentLT;
    private float bestLT;
    private float lastLT;

    public TrackManagement trackManagement;

    void Update()
    {
        if (trackManagement == null)
        {
            return;
        }

        if (trackManagement.currentLapTimer != currentLT)
        {
            currentLT = trackManagement.currentLapTimer;
            float minutes = Mathf.FloorToInt(currentLT / 60);
            float seconds = Mathf.FloorToInt(currentLT % 60);
            float milliSeconds = (currentLT % 1) * 1000;
            currentLapTime.text = string.Format("Time {0:00}.{1:00}.{2:000}", minutes, seconds, milliSeconds);
        }

        if (trackManagement.lastLapTimer != lastLT)
        {
            lastLT = trackManagement.lastLapTimer;
            float minutes = Mathf.FloorToInt(lastLT / 60);
            float seconds = Mathf.FloorToInt(lastLT % 60);
            float milliSeconds = (lastLT % 1) * 1000;
            lastLapTime.text = string.Format("Last lap {0:00}.{1:00}.{2:000}", minutes, seconds, milliSeconds);
        }

        if (trackManagement.bestLapTimer != bestLT)
        {
            bestLT = trackManagement.bestLapTimer;
            float minutes = Mathf.FloorToInt(bestLT / 60);
            float seconds = Mathf.FloorToInt(bestLT % 60);
            float milliSeconds = (bestLT % 1) * 1000;
            bestLapTime.text = string.Format("Best Lap {0:00}.{1:00}.{2:000}", minutes, seconds, milliSeconds);
        }
    }
}

Problem now is I am not getting either of the LastLap or Bestlap to record any time at all.

Sooo…

Issue was I didn’t use checkpoint through the lap so the time for both last and best lap would always be 00:00:000 with the timestamp i was using.