简体   繁体   中英

Getting error when attempting to save score?

The following code retrieves the player's score from the Google Play Leaderboard. If the retrieved value is superior to the one already stored on the device, the score is saved.

    public void Update()
    {       
PlayGamesPlatform.Instance.LoadScores(
            "myLeaderboardID",
            LeaderboardStart.PlayerCentered,
            100,
            LeaderboardCollection.Public,
            LeaderboardTimeSpan.AllTime,
            (data) =>
            {
                if (data.Valid)
                if (data.Scores[0].value > PlayerPrefs.GetInt("highScore", highScore))
                {
                    PlayerPrefs.SetInt("highScore", data.Scores[0].value);
                    PlayerPrefs.Save();
                }
            });
}

Unfortunately, I'm getting 2 errors on this line PlayerPrefs.SetInt("highScore", data.Scores[0].value);

error CS1502: The best overloaded method match for `UnityEngine.PlayerPrefs.SetInt(string, int)' has some invalid arguments

error CS1503: Argument `#2' cannot convert `long' expression to type `int'

How can I fix this?

You have to cast data.Scores[0].value (which is a long ) to Integer

You can do it quick and dirty like this:

PlayerPrefs.SetInt("highScore", (int)data.Scores[0].value);

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