[英]Unity Highscore reset at respawn
I'm trying to be able to respawn as well as reset the score counting as soon as i walk into my "Gold" Object. 当我走进“金”对象时,我试图重新生成以及重置分数计数。 As for now i'm not even able to respawn which was possible earlier before trying to implement the "Score-Stuff" (at first the "FoundGold" Script was only used to be able to respawn). 至于现在,我什至无法重生,这在尝试实现“ Score-Stuff”之前可能是更早的(起初,“ FoundGold”脚本仅用于重生)。 Also i'm trying to make the lowest Score the High-Score. 另外,我正在努力使最低分数成为高分。 Note that im new to C# and i kinda put everything together from the tutorials i needed so a answer with some actual code/stating where something went wrong would be much appreciated. 请注意,对于C#来说,我是新手,我将所需的所有教程都整理了一下,因此,对一些实际的代码/状态出了问题的答案,将不胜感激。
//GoldFound Code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GoldFound : MonoBehaviour
{
private ScoreManager theScoreManager;
public Transform target;
[SerializeField] private Transform player;
[SerializeField] private Transform respawnpoint;
private void Start()
{
theScoreManager = FindObjectOfType<ScoreManager>();
}
private void OnTriggerEnter(Collider other)
{
theScoreManager.scoreIncreasing = false;
player.transform.position = respawnpoint.transform.position;
theScoreManager.scoreCount = 0;
theScoreManager.scoreIncreasing = true;
}
}
other code 其他代码
//ScoreManager
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ScoreManager : MonoBehaviour
{
public Text scoreText;
public Text hiScoreText;
public float scoreCount;
public float hiScoreCount;
public float pointPerSecond;
public bool scoreIncreasing;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
if (scoreIncreasing)
{
scoreCount += pointPerSecond * Time.deltaTime;
}
if(scoreCount > hiScoreCount)
{
hiScoreCount = scoreCount;
}
scoreText.text = "Score: " + Mathf.Round (scoreCount);
hiScoreText.text = "High Score: " + Mathf.Round (hiScoreCount);
}
}
If you want to save your highscore in-between play sessions, then the easiest way to do so is to save the value to PlayerPrefs. 如果要在播放会话之间保存高分,则最简单的方法是将值保存到PlayerPrefs。 If you want to start saving more / more complex stuff you really should save it in a file you generate yourself. 如果您想开始保存更多/更复杂的内容,则应该将其保存在自己生成的文件中。 But in your case, PlayerPrefs is fine. 但就您而言,PlayerPrefs很好。
Here's a Unity tutorial on the subject: https://unity3d.com/learn/tutorials/topics/scripting/high-score-playerprefs 这是关于该主题的Unity教程: https : //unity3d.com/learn/tutorials/topics/scripting/high-score-playerprefs
Otherwise, you can just do it like this: 否则,您可以这样做:
public void SetHighscore (float currentScore)
{
if (PlayerPrefs.HasKey("highscore"))
{
float highscore = PlayerPrefs.GetFloat("highscore");
if (highscore > currentScore)
{
PlayerPrefs.SetFloat("highscore", currentScore);
PlayerPrefs.Save();
}
}
else
{
PlayerPrefs.SetFloat("highscore", currentScore);
PlayerPrefs.Save();
}
}
Then just write PlayerPrefs.GetKey("highscore") whenever you need it. 然后,只要需要就可以编写PlayerPrefs.GetKey(“ highscore”)。 (Though I'd also recommend you check if it exists by using the PlayerPrefs.HasKey("highscore")) https://docs.unity3d.com/ScriptReference/PlayerPrefs.html (尽管我也建议您使用PlayerPrefs.HasKey(“ highscore”)检查它是否存在。) https://docs.unity3d.com/ScriptReference/PlayerPrefs.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.