Search Unity

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

Discussion in 'Scripting' started by peyalakmp, Jan 20, 2020.

  1. peyalakmp

    peyalakmp

    Joined:
    Jan 20, 2020
    Posts:
    2
    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine.UI;
    3. using UnityEngine;
    4.  
    5. public class Timer : MonoBehaviour
    6. {
    7.     public Text timerText;
    8.     private float startTime;
    9.  
    10.     void Start()
    11.     {
    12.         startTime = Time.time;
    13.  
    14.     }
    15.  
    16.     // Update is called once per frame
    17.     void Update()
    18.     {
    19.         float t = Time.time = startTime;
    20.  
    21.         string minutes = ( (int) t / 60 ).ToString();
    22.         string seconds = ( t % 60 ).ToString();
    23.  
    24.         timerText.text = minutes + ":" + seconds;
    25.     }
    26. }
    27.  
     

    Attached Files:

    Last edited: Jan 20, 2020
  2. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    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.
     
    peyalakmp likes this.
  3. peyalakmp

    peyalakmp

    Joined:
    Jan 20, 2020
    Posts:
    2
    Thanks.Now the code works.
     
  4. WallaceT_MFM

    WallaceT_MFM

    Joined:
    Sep 25, 2017
    Posts:
    394
    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.
     
    peyalakmp likes this.