高分不更新c#
我有一个分数和高分的脚本,它似乎不起作用,没有错误,但是当玩家死亡时高分不会更新高分不更新c#,c#,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;
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));
}
}
}
使用系统集合;
使用UnityEngine.UI;
公共类OnGui2D:单一行为
{
公共静态ONGI2D OG2D;
公共静态积分;
int-hScore;
文本文本;
文本hText;
//用于初始化
无效开始(){
OG2D=这个;
得分=0;
hScore=PlayerPrefs.GetInt(“高分1”,0);
sText=GameObject.Find(“sText”).GetComponent();
hText=GameObject.Find(“sText”).GetComponent();
}
//每帧调用一次更新
无效更新(){
sText.text=(“”+(分数+0));
hText.text=(“”+hScore);
}
无效OnTriggerEnter2D(碰撞的R2D列)
{
如果(col.gameObject.tag==“硬币”){
分数=分数+1;
}否则如果(col.gameObject.tag==“敌人”){
OnGui2D.OG2D.CheckHighScore();
}
}
公共无效检查高分(){
如果((分数+0)>hScore){
Log(“保存高分”);
PlayerPrefs.GetInt(“高分1”(分数+0));
}
}
}
您确定要获取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));
}
}