简体   繁体   中英

Highscore in endless runner

Does anyone know why the highscore isn't this working? In game, the score system works but the highscore isn't. I've tried many ways to make it work, but it doesn't.

using UnityEngine;
using UnityEngine.UI;
using System;

public class score : MonoBehaviour {

    public Transform jugador;
    public Text Score;
    public Text highscore;
    public Text scoreText;
    private int currentscore = 0;
    private float floa;


    void Start() {

        highscore.text = PlayerPrefs.GetInt("HighScore", 0).ToString();
    }
    private void SetHighScore() {

        if (currentscore > PlayerPrefs.GetInt("HighScore", 0)) {
            PlayerPrefs.SetInt("HighScore", currentscore);
        }
    }
    private void Update () {

        floa = jugador.position.z;
        currentscore = (int)floa;
        scoreText.text = currentscore.ToString();
        highscore.text = PlayerPrefs.GetString ("HighScore").ToString();
    }


}

The highscore is working but when you use it on the highscore.text you are calling a PlayerPrefs.GetString but the Highscore it's actually an integer: Just change the last line in this way:

highscore.text = PlayerPrefs.GetInt("HighScore",0).ToString();

It should be fine:D

highscore.text = PlayerPrefs.GetString ("HighScore").ToString();

above line is wrong it should be

highscore.text = PlayerPrefs.GetInt("HighScore",0).ToString();

becasue when you save score PlayerPrefs is Int type not the string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM