高分不更新c#

本文关键字:更新 | 更新日期: 2023-09-27 18:05:22

我有一个分数和高分脚本,似乎不工作,没有错误,但高分不更新当玩家死亡。

using System.Collections;
using UnityEngine.UI;
public class OnGui2D : MonoBehaviour
{    
     public static OnGui2D OG2D;
     public static int score;
     int hScore;
     Text sText;
     Text hText;
     // Use this for initialization
     void Start () {
         OG2D = this;
         score = 0;
         hScore = PlayerPrefs.GetInt ("HighScore1", 0);
         sText = GameObject.Find ("sText").GetComponent<Text> ();
         hText = GameObject.Find ("sText").GetComponent<Text> ();
     }
     // Update is called once per frame
     void Update () {
         sText.text = ("" + (score + 0));
         hText.text = ("" + hScore);
     }
     void OnTriggerEnter2D(Collider2D col) 
     {
         if (col.gameObject.tag == "Coin") {
             score = score + 1;
         } else if (col.gameObject.tag == "Enemy") {
             OnGui2D.OG2D.CheckHighScore();

         }
     }
     public void CheckHighScore(){
         if ((score + 0) > hScore) {
             Debug.Log ("Saving highscore");
             PlayerPrefs.GetInt ("HighScore1", (score + 0));             
         }     
     }         
}

高分不更新c#

您确定要获取int而不设置它吗?为什么总是加零?

     public void CheckHighScore(){
     if ((score + 0) > hScore) {
         Debug.Log ("Saving highscore");
         PlayerPrefs.GetInt ("HighScore1", (score + 0));             
     }     
 }

->

     public void CheckHighScore(){
     if ((score + 0) > hScore) {
         Debug.Log ("Saving highscore");
         PlayerPrefs.SetInt ("HighScore1", (score + 0));             
     }     
 }