高分不更新c#

时间:2015-07-29 08:41:43

标签: c#

我有一个得分和高分的脚本似乎不起作用没有错误但是当玩家死亡时高分不会更新。

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));             
         }     
     }         
}

1 个答案:

答案 0 :(得分:1)

你确定要获取int并且不设置它吗?为什么你总是加零?

     public void CheckHighScore(){

     if ((score + 0) > hScore) {

         Debug.Log ("Saving highscore");
         PlayerPrefs.GetInt ("HighScore1", (score + 0));             
     }     
 }

- &GT;

     public void CheckHighScore(){

     if ((score + 0) > hScore) {

         Debug.Log ("Saving highscore");
         PlayerPrefs.SetInt ("HighScore1", (score + 0));             
     }     
 }