How do I keep the highest scoring value for a game session?

Hi guys.

I have a problem with my little scoring system. Wonder if u guys can help me out. Once again, I’ve been following the tutorials on catlikecoding.com to sharpen up my C# skills.

My scoring system is based on how far a player has traveled (like the function used in the tutorial)

In my Runner.cs Script:

distanceTraveled = transform.localPosition.x;
GUIManager.SetDistance (distanceTraveled);

Now I have managed to add a new variable for high score on a guitext which displays my last score achieved after the game over screen and shows up when u reset the scene. But, here’s my problem and concern : Whenever the game ends now and the scene resets it uses the previous score achieved and doesn’t keep the highest value achieved, meaning I might was well rename it to previous score instead of high score.

In my Runner.cs Script for setting score:

highScore = distanceTraveled;

If (distanceTraveled > highScore) {
    GUIManager.SetScore (highScore);
}

else if (distanceTraveled < highScore) {
//(I haven't set anything here as I'm not sure wtf I can use here, and believe me I have tried)
}

In my GUIManager.cs Script:

public static void SetScore (float highScore) {
    instance.highScore.text = highScore.ToString ("f0");
}

So my question is, how do I keep the highest distance traveled value in my high score variable for the remainder of that game session?

PS: Once again, if I don’t make any sense, just curse me. Have to do this from my phone again seeing as this god forsaken country haven’t installed my internet line yet.

Thanks in advance.

This test will always fail. By assigning the same value to two different variables it is impossible for one to be greater than the other.

highScore = distanceTraveled;

if (distanceTraveled > highScore) {
    GUIManager.SetScore (highScore);
}

Instead use this:

if (distanceTraveled > highScore) {
    highScore = distanceTraveled;
    GUIManager.SetScore (highScore);
}

Just be sure to initialize highscore (to 0 or the saved highscore) in Start()/Awake() or somewhere else outside of the update loop.

Im not to sure how to do this in Unity, I believe you just transfer the int (or float) between scripts. otherwise, you can create a CSV file as an alternative (using File.writeLine(location, string)), and it will always contain the values for you. if you do try this, you will need “Using System.IO”, although I would only recommend this as a last resort. I’m sure Unity has a built in method, or an alternative to approaching this.