Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Help With Saving Time / Score in Player Prefs. c#

Discussion in 'Scripting' started by The-Sauce, Apr 17, 2014.

  1. The-Sauce

    The-Sauce

    Joined:
    Feb 28, 2014
    Posts:
    16
    I'm displaying a Timer that shows how long you have been alive for, and its worked fine. I just can't seem to figure out how to save this time and create a Total Time Survived kind of score. Right now it shows the same exact timer, one on top of the other. I have looked around and just can't find a solution to this. I've gotten errors with coverting float to ints a few times, mostly it works, it just doesn't add up the time, resets each time the game is started. Can somebody please point me in the right direction, this is my script as of right now in c#.

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class DistanceCS : MonoBehaviour {
    5.  
    6.     float runTime;
    7.  
    8.     void Start() {
    9.         PlayerPrefs.GetFloat ("runTime", runTime);
    10.     }
    11.     void OnDestroy() { // Set when my player dies
    12.         PlayerPrefs.SetFloat ("runTime", runTime + Time.timeSinceLevelLoad);
    13.     }
    14.    
    15.     void OnGUI () {
    16.         guiText.text = "Time: " + Time.timeSinceLevelLoad.ToString ("F2") + "\nTotal Time: " + runTime + Time.timeSinceLevelLoad.ToString ("F2");
    17.            
    18.     }  
    19. }
     
    Last edited: Apr 17, 2014
  2. cherub

    cherub

    Joined:
    Apr 26, 2006
    Posts:
    493
    Not sure if this is what you are looking for but this worked. Each time i run the game the Log returns a higher number. Just figure out how to pipe it into your OnGUI

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class DistanceCS : MonoBehaviour {
    5.    
    6.     //float runTime;
    7.  
    8.     void Start() {
    9.         //PlayerPrefs.GetFloat ("runTime", runTime);
    10.     }
    11.  
    12.     void OnDestroy() { // Set when my player dies
    13.         var savedTime = PlayerPrefs.GetFloat ("runTime");
    14.         PlayerPrefs.SetFloat ("runTime", savedTime + Time.timeSinceLevelLoad);
    15.  
    16.         Debug.Log (PlayerPrefs.GetFloat ("runTime"));
    17.     }
    18.  
    19.     //void OnGUI () {
    20.     //  guiText.text = "Time: " + Time.timeSinceLevelLoad.ToString ("F2") + "\nTotal Time: " + runTime + Time.timeSinceLevelLoad.ToString ("F2");
    21.        
    22.     //}  
    23. }
     
  3. The-Sauce

    The-Sauce

    Joined:
    Feb 28, 2014
    Posts:
    16
    Perfect, thank you I really appreciate it. I'll post the full code for all to see

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class DistanceCS : MonoBehaviour {
    5.    
    6.     float runTime;
    7.    
    8.     void Start() {
    9.         PlayerPrefs.GetFloat ("runTime", runTime);
    10.     }
    11.    
    12.     void OnDestroy() { // Set when my player dies
    13.         var savedTime = PlayerPrefs.GetFloat ("runTime");
    14.         PlayerPrefs.SetFloat ("runTime", savedTime + Time.timeSinceLevelLoad);
    15.        
    16.         Debug.Log (PlayerPrefs.GetFloat ("runTime"));
    17.     }
    18.    
    19.     void OnGUI () {
    20.         guiText.text = "Time: " + Time.timeSinceLevelLoad.ToString ("F2") + "\nTotal Time: " + PlayerPrefs.GetFloat ("runTime");
    21.             //+ "\nTotal Time: " + runTime + Time.timeSinceLevelLoad.ToString ("F2");
    22.     }
    23. }