Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

c# "Stop Timer" save time is not saving and key is not storing

Discussion in 'Scripting' started by Andreas12345, Sep 12, 2014.

  1. Andreas12345

    Andreas12345

    Joined:
    Oct 17, 2013
    Posts:
    526
    Hi,
    i want to make a button, with this button i want to save the time from my timer:
    Code (csharp):
    1.  
    2.   if (GUI.Button(new Rect(150, 10, 60, 30), "Stop")) //left down width height
    3.         {
    4.             StopMainTimer();
    5.             float elapsedTime = _mainTimer; //timePlayTime
    6.             string timeKey = Application.loadedLevelName + "_time";
    7.        
    8.  
    9.             if (PlayerPrefs.HasKey(timeKey))
    10.             {
    11.                 float previousTime = PlayerPrefs.GetFloat(timeKey);
    12.                 if (elapsedTime < previousTime)
    13.                 {
    14.                     PlayerPrefs.SetFloat(timeKey, elapsedTime);
    15.                 }
    16.                 else
    17.                 {
    18.                     PlayerPrefs.SetFloat(timeKey, elapsedTime);
    19.                 }
    20.                 print("Time: " + elapsedTime);
    21.  
    22.  
    23.             }
    24.         }
    25.  
    I do not find an error in this script, it should be print the elapsedTime, but it does not.
    The MainTimer is stopping and removed from the GUI... Did i miss something here?
    Also the timekey is not set in the registry.
     
  2. orb

    orb

    Joined:
    Nov 24, 2010
    Posts:
    3,037
    One thing I notice is that you set the key to elapsedTime no matter what.
     
  3. Andreas12345

    Andreas12345

    Joined:
    Oct 17, 2013
    Posts:
    526
    i replace:
    Code (csharp):
    1.  float elapsedTime = _mainTimer; //timePlayTime
    with
    Code (csharp):
    1.  float elapsedTime = Time.time - _mainTimer; //timePlayTime
    but the same issue :(
     
  4. BmxGrilled

    BmxGrilled

    Joined:
    Jan 27, 2014
    Posts:
    238
    This part:
    1. if (elapsedTime < previousTime)
    2. {
    3. PlayerPrefs.SetFloat(timeKey, elapsedTime);
    4. }
    5. else
    6. {
    7. PlayerPrefs.SetFloat(timeKey, elapsedTime);
    8. }
    ...is setting the key whether time has elapsed or not, my guess is take out the first SetFloat line, leave the second one in, hope this helps! :)
     
  5. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    Code (csharp):
    1. if(PlayerPrefs.HasKey(timeKey))
    If you have never successfully saved to PlayerPrefs, this key never gets made. So you never get to continue to save to PlayerPrefs thus the key is still not made.
     
  6. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    Looking at the code, I think you attached your else to the wrong if statement. I reordered your brackets around a little.
    Code (csharp):
    1. if (PlayerPrefs.HasKey(timeKey))
    2. {
    3.   // Player has a previous fastest time. See if we beat it.
    4.   float previousTime = PlayerPrefs.GetFloat(timeKey);
    5.   if (elapsedTime < previousTime) // Beat previous time!
    6.     PlayerPrefs.SetFloat(timeKey, elapsedTime);
    7. }
    8. else // Player has no time saved
    9. {
    10.   PlayerPrefs.SetFloat(timeKey, elapsedTime);
    11. }
     
  7. Andreas12345

    Andreas12345

    Joined:
    Oct 17, 2013
    Posts:
    526
    I will check this, thx.