简体   繁体   中英

How to save high score?

I am trying to save high score in my game. The high score is updated with the score during play. However, after level restarts, both (current score and high score) becomes zero.

How can I do this? What mistake I am doing?

Here is my code:

Generate

public class Generate : MonoBehaviour 
{    
    private static int score;
    public GameObject birds;
    private string Textscore;
    public GUIText TextObject;
    private int highScore = 0;
    private int newhighScore;
    private string highscorestring;
    public GUIText highstringgui;

    // Use this for initialization
    void Start()
    {
        PlayerPrefs.GetInt ("highscore", newhighScore);
        highscorestring= "High Score: " + newhighScore.ToString();
        highstringgui.text = (highscorestring);
        InvokeRepeating("CreateObstacle", 1f, 3f);
    }

    void Update()
    {
        score = Bird.playerScore; 
        Textscore = "Score: " + score.ToString();
        TextObject.text = (Textscore);
        if (score > highScore) 
        {
            newhighScore=score;
            PlayerPrefs.SetInt ("highscore", newhighScore);
            highscorestring = "High Score: " + newhighScore.ToString ();
            highstringgui.text = (highscorestring);
        } 
        else 
        {
            PlayerPrefs.SetInt("highscore",highScore);
            highscorestring="High Score: " + highScore.ToString();
            highstringgui.text= (highscorestring);
        }
    }

    void CreateObstacle()
    {
        Instantiate(birds);
    }
} 

Bird

public class Bird : MonoBehaviour {
    public GameObject deathparticales;
    public Vector2 velocity = new Vector2(-10, 0);
    public float range = 5;
    public static int playerScore = 0;

    // Use this for initialization
    void Start()
    {    
        rigidbody2D.velocity = velocity;
        transform.position = new Vector3(transform.position.x, transform.position.y - range * Random.value, transform.position.z);      
    }

    // Update is called once per frame
    void Update () { 
        Vector2 screenPosition = Camera.main.WorldToScreenPoint(transform.position);
        if (screenPosition.x < -10)
        {
            Die();
        }
    }    
    // Die by collision
    void OnCollisionEnter2D(Collision2D death)
    {
            if(death.transform.tag == "Playercollision")
        {
            playerScore++;
            Destroy(gameObject);
            Instantiate(deathparticales,transform.position,Quaternion.identity);
        }
    }

    void Die()
    {
        playerScore =0;
        Application.LoadLevel(Application.loadedLevel);    
    }
}

The problem is your variable highScore. It is always 0 . In the game you ask

if (score > highScore)

And because you set highScore = 0 while declaring that variable, score is always greater.

My suggestion is that you should declare it without value:

private int highScore;

In Start() you should give it value of saved high-score if it exists, and if it doesn't, give it 0 value:

highScore = PlayerPrefs.GetInt("highscore", 0);

That should work for you.

This line in Start(), won't actually do anything.

PlayerPrefs.GetInt ("highscore", newhighScore);

The second parameter is the default return value, if the given key doesn't exist. But you're not using the return value for anything.

I think what you meant to do is:

newhighScore = PlayerPrefs.GetInt("highscore");

The default value will be 0, when not set explicitly.

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