I am using Time.time but it shows error.Can anyone tell me what is wrong with the code?

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

public class Timer : MonoBehaviour
{
    public Text timerText;
    private float startTime;

    void Start()
    {
        startTime = Time.time;

    }

    // Update is called once per frame
    void Update()
    {
        float t = Time.time = startTime;

        string minutes = ( (int) t / 60 ).ToString();
        string seconds = ( t % 60 ).ToString();

        timerText.text = minutes + ":" + seconds;
    }
}

Hi

Please format you code with code tags - you can edit your post with edit button.

Your error message tells you exactly what is wrong - you can’t assign to Time.time value, like you are doing in your code. It is a read only value, like the error message says.

1 Like

Thanks.Now the code works.

float t = Time.time = startTime;
This line is the problem. Assuming you want to assign Time.time to startTime, and t, just switch the order:
float t = startTime = Time.time;
Assignments happen right to left. You can’t assign to Time.time, so that caused the error.

1 Like