- Home /
Best Distance (Record) System on Infinite Runner game
I'm Brazillian. Dont look for errors!
I'm making an Infinite Runner game on Unity, and I want to make an Record system, measured by the distance. Here's the script I made based on a YouTube tutorial:
#pragma strict
var StartPOS: Vector3;
var Record: float;
var Distance: float;
var CustomSkin: GUISkin;
var pause : boolean = false;
var pauseGUI : GUIText;
pauseGUI.enabled = false;
function Start () {
StartPOS = transform.position;
Record = PlayerPrefs.GetInt("Reccord");
}
function Update(){
}
function OnGUI () {
Distance = transform.lossyScale.magnitude;
Distance = Vector3.Distance(transform.position,StartPOS);
GUI.skin = CustomSkin;
GUI.Label(new Rect(Screen.width/10,Screen.height/10,Screen.width/1,Screen.width/10),"Distance (m): "+ Distance.ToString("F1"));
GUI.skin = CustomSkin;
GUI.Label(new Rect(Screen.width/10,Screen.height/5,Screen.width/1,Screen.width/10),"Best (m): "+ Record.ToString("F1"));
if (Record<Distance){
PlayerPrefs.GetInt("Reccord",Mathf.Floor (Distance));
}
}
function OnMouseOver () {
guiText.material.color = Color.yellow;
}
function OnMouseExit () {
guiText.material.color = Color.white;
}
function OnMouseDown () {
Time.timeScale = 1.0;
pauseGUI.enabled = false;
Destroy(this);
Application.LoadLevel ("InfiniteUnity");
}
On the GUI it appears like this:
And in the editor it appears like this:
This means that the game is not measuring the record distance, for some reason. Can someone help me? Thanks a lot!
Not sure of your exact problem, but I see two issues. The value assigned in line 21 is completely overwritten in line 22, so line 21 is useless. Distance is an int, so on line 22, if the distance calculation is less than 1.0, it will evaluate to the int 0. Note the use of upper case 'Distance' is confusing, especially since you are using Vector3.Distance() as well.. By convention, variables should start with a lower case letter.
robertbu, I know, but the Distance is being measured well. The problem is the Reocrd lines. Correct me if I'm wrong, please
AndroidWG can i have your email, i have some questions :))
Answer by TruffelsAndOranges · Jul 07, 2015 at 02:00 PM
PlayerPrefs.GetInt("Reccord",Mathf.Floor (Distance)); ... pretty sure that should be SetInt and not GetInt.
It also seems strange to be storing a float as an int. The user will see the record being broken, only to find that the new record isn't being saved,
Your answer
![](https://koobas.hobune.stream/wayback/20220613155334im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Problem with the Playerprefs 1 Answer
update highscore if current is higher than previous score 0 Answers
[SOLVED] A Playerpref in one place is blocking all my other Playerprefs 1 Answer
Playerprefs not work!? 0 Answers
[SOLVED] Same script (virtually) on two gameObjects. One saves to playerprefs, one doesn't 2 Answers