Search Unity

Sending distance of character to my highscore

Discussion in 'Scripting' started by DarkNeo, Aug 2, 2014.

  1. DarkNeo

    DarkNeo

    Joined:
    Apr 27, 2014
    Posts:
    53
    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.

    Code (csharp):
    1.  
    2. var myTransform : Transform;
    3. var origPos : Vector3;
    4. var currentPos : Vector3;
    5. var distance : float = 0;
    6.  
    7. static var distanceCount : int;
    8.  
    9. function Start()
    10. {
    11.    
    12.     origPos=myTransform.position;
    13.     currentPos=myTransform.position;
    14.    
    15. }
    16.  
    17. function Update ()
    18. {
    19.     currentPos=myTransform.position;
    20.     Debug.Log((currentPos-origPos).magnitude);
    21.     distance += transform.position.x;
    22.     guiText.text = "Distance: " + myTransform.position.x.ToString("f2");
    23.    
    24.    
    25.     {
    26.  
    27. if (distance > PlayerPrefs.GetInt("score"))
    28.     {
    29.         PlayerPrefs.GetInt("score");
    30.         PlayerPrefs.SetInt("score",distance);
    31.     }
    32.  
    33.  
    34.     }
    35.    
    36. }
    37.  

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


    Code (csharp):
    1.  
    2. function OnGUI () {
    3.  
    4.     guiText.text = "Highscore : " + PlayerPrefs.GetInt("score");
    5.  
    6. }
    7.  

    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
     
  2. hammil

    hammil

    Joined:
    Jun 5, 2013
    Posts:
    56
    Can you explain exactly what doesn't work with your current solution?
     
  3. DarkNeo

    DarkNeo

    Joined:
    Apr 27, 2014
    Posts:
    53
    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.
     

    Attached Files:

  4. DarkNeo

    DarkNeo

    Joined:
    Apr 27, 2014
    Posts:
    53
    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.

    Code (JavaScript):
    1. var score : GUIText;
    2. var highScore : GUIText;
    3. var myTransform : Transform;
    4. var origPos : Vector3;
    5. var currentPos : Vector3;
    6. var distance : int;
    7. function Start()
    8. {
    9.     highScore.text = "Highscore: " + PlayerPrefs.GetInt("score").ToString();
    10.    
    11. }
    12.  
    13. {
    14.     origPos=myTransform.position;
    15.     currentPos=myTransform.position;
    16. }
    17. function Update()
    18. {
    19.     currentPos=myTransform.position;
    20.     Debug.Log((currentPos-origPos).magnitude);
    21.     distance += transform.position.x;
    22.    
    23.     score.text = "Score: " + myTransform.position.x.ToString("f2");
    24. }
    25.  
    26.  
    27. if (distance > PlayerPrefs.GetInt("score"))
    28.     {
    29.         PlayerPrefs.GetInt("score");
    30.         PlayerPrefs.SetInt("score",distance);
    31.     }
     
unityunity