Sending distance of character to my highscore

Hi there,

I am having a little bit of trouble with sending the distance that my character has reached into my highscore.

I am using Player Prefs to save the highscore.

Here is my script for my DistanceCounter which is attached to my guiText.

var myTransform : Transform;
var origPos : Vector3;
var currentPos : Vector3;
var distance : float = 0;

static var distanceCount : int;

function Start()
{
   
    origPos=myTransform.position;
    currentPos=myTransform.position;
   
}

function Update () 
{
    currentPos=myTransform.position;
    Debug.Log((currentPos-origPos).magnitude);
    distance += transform.position.x;
    guiText.text = "Distance: " + myTransform.position.x.ToString("f2");
   
   
    {

if (distance > PlayerPrefs.GetInt("score"))
    {
        PlayerPrefs.GetInt("score");
        PlayerPrefs.SetInt("score",distance);
    }


    }
   
}

Here is my Highscore script which is attached to another guiText.

function OnGUI () {

    guiText.text = "Highscore : " + PlayerPrefs.GetInt("score");

}

My high score will count up in seconds and save it perfectly with PlayerPrefs.

Can anyone help me so when my character reaches a certain distance it will display that in the high score? or point me in the right direction?

PS: Using Javascript

Thank you

Can you explain exactly what doesn’t work with your current solution?

I have attached a screen shot.

My Score adds up as the character moves across the screen in the x direction, currently my High score just counts up in seconds. I am trying to get it so the score for the character distance will be sent to the High score. Still trying to figure it out.

EDIT: Sorry for the double screenshot post.


I am trying to currently use another script so the score and highscore is all in one.

I still cannot get my score to send to my highscore, really not sure what I am doing wrong.

var score : GUIText;
var highScore : GUIText;
var myTransform : Transform;
var origPos : Vector3;
var currentPos : Vector3;
var distance : int;
function Start()
{
    highScore.text = "Highscore: " + PlayerPrefs.GetInt("score").ToString();
   
}

{
    origPos=myTransform.position;
    currentPos=myTransform.position;
}
function Update()
{
    currentPos=myTransform.position;
    Debug.Log((currentPos-origPos).magnitude);
    distance += transform.position.x;
   
    score.text = "Score: " + myTransform.position.x.ToString("f2");
}


if (distance > PlayerPrefs.GetInt("score"))
    {
        PlayerPrefs.GetInt("score");
        PlayerPrefs.SetInt("score",distance);
    }