Why does the high score reset every time?

It lives in memory. Memory ends when the game does.

Why does the high score reset every time?

The problem

A field forgets.

public int Best { get; private set; } void GameOver( int coins ){    if ( coins > Best ) Best = coins;}

Best is right until you quit. Next launch, it's 0 again.

The fix

Write it to the player's disk.

FileSystem.Data is a folder kept for your game on the player's machine. Write there, read it back on start.

WriteAllText→on their disk→ReadAllText

The fix in code

Read on start. Write on a new best.

const string Save = "best.txt"; protected override void OnStart(){    if ( !FileSystem.Data.FileExists( Save ) ) return;    Best = int.Parse( FileSystem.Data.ReadAllText( Save ) );} void GameOver( int coins ){    if ( coins <= Best ) return;    Best = coins;    FileSystem.Data.WriteAllText( Save, Best.ToString() );}

Our trucking game keeps its best score in exactly this file.

Watch out

A file can be missing or garbled.

First launch has no file. One edited by hand might not parse.

First launch
no file yet
Edited by hand
may not parse
So
FileExists, and try catch

The rule

Should it survive a restart? Write it down.

Save
FileSystem.Data.WriteAllText
Load
ReadAllText, on start
Guard
FileExists, try catch