gpt4 book ai didi

c# - 使用 PlayerPrefs 的 Unity Highscore 无法正常工作

转载 作者:太空宇宙 更新时间:2023-11-03 18:20:42 25 4
gpt4 key购买 nike

这可能是一个非常简单的解决方案和问题,但我一直在为此绞尽脑汁。每次我统一测试这个,如果我得到的分数低于高分,高分无论如何都会改变。这是我的代码。如果您需要更多,请告诉我。

脚本 1:

using UnityEngine;
using UnityEngine.SceneManagement;
public class CheckConeDeath : MonoBehaviour {
public PlayerMovement movement;

void OnCollisionEnter(Collision CollisionInfo){
if(CollisionInfo.collider.tag=="Cone"){
movement.enabled = false;
GetComponent<Rigidbody>().constraints = RigidbodyConstraints.None;
Invoke ("EndGame", 1.5f);
}
}
void EndGame(){
PlayerPrefs.SetFloat ("finalScore", Mathf.RoundToInt(FindObjectOfType<DistanceScore>().plrz));
PlayerPrefs.Save ();

Debug.Log (PlayerPrefs.GetFloat("finalScore"));
SceneManager.LoadScene (SceneManager.GetActiveScene ().buildIndex + 1);
}
}

脚本 2:

using UnityEngine;
using UnityEngine.UI;
public class finalscore : MonoBehaviour {
public Text text;

public Text highscore;
void Start(){
Debug.Log (Mathf.Round (PlayerPrefs.GetFloat ("finalScore")));
Debug.Log (PlayerPrefs.GetInt ("hs", 0));
Debug.Log (Mathf.Round (PlayerPrefs.GetFloat ("finalScore")) > PlayerPrefs.GetInt ("hs", 0));
// PlayerPrefs.SetFloat ("hs", Mathf.Round(PlayerPrefs.GetFloat("finalScore")));
highscore.text = "All time High Score: "+Mathf.Round (PlayerPrefs.GetFloat ("finalScore")).ToString ();
text.text = "Final Score: " + Mathf.Round(PlayerPrefs.GetFloat("finalScore"));
if(Mathf.Round(PlayerPrefs.GetFloat("finalScore"))<PlayerPrefs.GetInt("hs",0)){
PlayerPrefs.SetFloat ("hs", Mathf.Round (PlayerPrefs.GetFloat ("finalScore")));
highscore.text = "All time High Score: "+Mathf.Round (PlayerPrefs.GetFloat ("finalScore")).ToString ();
}
}
}

这是发生了什么的截图:

之前: enter image description here

之后: enter image description here

我的高分怎么可能下降了!?非常感谢您的宝贵时间!

最佳答案

我认为你在此处放置了错误的逻辑,假设 finalScore 是在该级别获得的分数,而 hs 是历史最高分

您在脚本 2 中编写了这一行

(Mathf.Round(PlayerPrefs.GetFloat("finalScore"))<PlayerPrefs.GetInt("hs",0)

改成这样

(Mathf.Round(PlayerPrefs.GetFloat("finalScore"))>PlayerPrefs.GetInt("hs",0)

Note that Greater than symbol

关于c# - 使用 PlayerPrefs 的 Unity Highscore 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56519755/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com