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 Setting High Score Float C#

Discussion in 'Editor & General Support' started by The-Sauce, May 1, 2014.

  1. The-Sauce

    The-Sauce

    Joined:
    Feb 28, 2014
    Posts:
    16
    I've tried a number of different things and I can't figure this one out either. Now I'm just confusing myself and doing more harm than good, and I was hoping someone else could lend a hand. I figured out how to save total time in this thread, http://forum.unity3d.com/threads/241060-Help-With-Saving-Time-Score-in-Player-Prefs-c , but now I'd like to save the Best / Highest Time as a high score and I can't seem to figure it out. Can someone point me in the right direction using c#, I've tried a number of things that I found in threads here and I know I'm doing something wrong.

    Here is my code:

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class DistanceCS : MonoBehaviour {
    5.    
    6.     public float runTime; ;
    7.     public float bestTime;
    8.  
    9.     void Start() {
    10.                 PlayerPrefs.GetFloat ("runTime", runTime);
    11.                 PlayerPrefs.GetFloat ("bestTime", bestTime);
    12.                 if (PlayerPrefs.GetFloat ("runTime") > bestTime) {
    13.                         bestTime = runTime;
    14.                 } else
    15.                 bestTime = bestTime;
    16.         }
    17.    
    18.     void OnDestroy() { // Set when my player dies
    19.  
    20.  
    21.         PlayerPrefs.SetFloat ("runTime", Time.timeSinceLevelLoad);
    22.         PlayerPrefs.SetFloat ("bestTime", bestTime);
    23.        
    24.         //var savedTime = PlayerPrefs.GetFloat ("runTime");
    25.     //PlayerPrefs.SetFloat ("runTime", savedTime + Time.timeSinceLevelLoad);
    26.     //Debug.Log (PlayerPrefs.GetFloat ("runTime"));
    27.        // ^ I had used these before to save the total time
    28.     }
    29.    
    30.     void OnGUI () {
    31.         guiText.text = "Time: " + Time.timeSinceLevelLoad.ToString ("F2") + "\nBest Time: " + PlayerPrefs.GetFloat ("bestTime");
    32.     }
    33. }
     
  2. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    The second parameter of the get-function takes an default-value for non-existent/non-initialized values. It's not going to return the entry's value into the variable you put there.

    In order to assign your values to your variables runTime and bestTime, use the return value the function offers.

    Code (csharp):
    1.  
    2. void Start()
    3. {
    4.                 runTime = PlayerPrefs.GetFloat ("runTime");
    5.                 bestTime = PlayerPrefs.GetFloat ("bestTime");
    6.                 // now you can alsp use the variables instead of calling the get function again
    7.                 // as for your way you implement it, the runTime seems to be the last runTime which was saved,
    8.                 // that means your bestTime will only be updated when this scene will be started once again (if the last runTimeis greater than the bestTime)
    9.                 // try to place the comparison (of the latest runTime (Time.timeSinceLevelLoad) with the bestTime) in the OnDestroy function
    10.                 // so the runTime of your current run, which has just ended, will be compared to the bestTime and can be saved as new bestTime
    11.  
    12.                 if (runTime > bestTime)  
    13.                         bestTime = runTime;
    14.                 else // this is actually doing nothing, you can remove the else and it's code
    15.                         bestTime = bestTime;
    16.         }
    17.  
     
  3. The-Sauce

    The-Sauce

    Joined:
    Feb 28, 2014
    Posts:
    16
    Hey thanks a lot Suddoha, I appreciate your commented out help on this. I try cramming too much work/ study at once and you guys are big help.