Search Unity

Question How do I record and save time durations between certain events?

Discussion in 'Scripting' started by bomogyoro, Jan 30, 2023.

  1. bomogyoro

    bomogyoro

    Joined:
    Jan 14, 2021
    Posts:
    1
    Hi! I'm new to unity and I'm trying to make a game in which you are collecting balls. I would like to record the time for each ball collected. So for example, the game starts, a ball appears somewhere, I collect it and the time is recorded. Then the time resets, and another time period is recorded once I collected the next ball. And so on. (The recorded time would not be displayed for the player but would be saved locally.)
    Could you please advise me on how to employ this in my script? Thank you in advance!

    Code (CSharp):
    1. public List<GameObject> spheresToCollect;
    2.  
    3.     void Start()
    4.     {
    5.  
    6.         spheresToCollect[0].SetActive(true);
    7.  
    8.         for(int i=1; i < spheresToCollect.Count; i++)
    9.         {
    10.             spheresToCollect[i].SetActive(false);
    11.         }
    12.  
    13.     }
    14.    
    15.     void Update()
    16.     {
    17.        
    18.     }
    19.  
    20.  
    21.     public void SpherePickup(GameObject sphere)
    22.     {
    23.         sphere.SetActive(false);
    24.         spheresToCollect.RemoveAt(0);
    25.  
    26.         if (spheresToCollect.Count > 0)
    27.         {
    28.             NextSphere();
    29.         }
    30.         else
    31.         {
    32.             LastSphere();
    33.         }
    34.  
    35.     }
    36.  
    37.  
    38.     void NextSphere()
    39.     {
    40.         spheresToCollect[0].SetActive(true);      
    41.     }
    42.  
    43.     void LastSphere()
    44.     {
    45.         spheresToCollect[0].SetActive(true);      
    46.     }
    47.  
    48.  
     
  2. TzuriTeshuba

    TzuriTeshuba

    Joined:
    Aug 6, 2019
    Posts:
    185
    first of all, this will throw an error since you are accesing the first element of a list with no elements.
    Code (CSharp):
    1.     public void SpherePickup(GameObject sphere)
    2.     {
    3.         sphere.SetActive(false);
    4.         spheresToCollect.RemoveAt(0);
    5.         if (spheresToCollect.Count > 0)
    6.         {
    7.             NextSphere();
    8.         }
    9.         else
    10.         {
    11.             LastSphere();// <--- error
    12.         }
    13.     }

    but for your question, do you need to keep the time that ball #2 was collected forever? or once ball #3 is collected, you can dispose of it? regardless, you can get the "Game Time" with either of the 2 values
    Code (CSharp):
    1. Time.time //affected by time scale
    2. Time.unscaledTime //unaffected by time scale
    if you never change your games time scale then they should be the same i think, but you may change the time scale for things like pausing (set time scale to 0), or some cool affects to slow or speed up things. you just need to consider if you want those time scale changes to affect the time you are recording.

    so if you record
    Code (CSharp):
    1. float timeLastBallWasCollected = Time.time
    when ball #1 is collected, then when ball #2 is collected, the time difference will be
    Code (CSharp):
    1. float timeDifference = Time.time - timeLastBallWasCollected;
     
    Kurt-Dekker likes this.