每秒减少一个定时器,两个整数都在减少

Decreasing a Timer every Second, both integers are decreasing

我试图在我的游戏中实现一个分数系统。我有两个变量,

public int maxScore; 
public int currentScore;

要设置 currentScore,我获取 maxScore 并将其放入 currentScore:

currentScore = maxScore;

因此,如果玩家开始游戏,他将获得最多的分数。如果他现在要慢下来,我会减少 currentPoints per Second with

IEnumerator CurrentScore()
{
        currentScore -= 1;
        yield return new WaitForSeconds(5); //I'm using 5, because 1 does not equals a second
}

然后说

StartCoroutine(CurrentScore());

如果我现在查看游戏,一切看起来都很好:

游戏开始时,两者都在 3000:

如果计时器运行到特定值,currentScore 减少,但 MaxScore 保持不变。应该是:

但是,一旦 WinScreen 弹出,两个值都开始快速下降,而不是只有一个下降:

我将此脚本附加到显示当前(已获得)积分和可能积分的文本中:

using UnityEngine;
using UnityEngine.UI;

public class WinScreenScore : MonoBehaviour {
    //Load the classes
    public GameManager manager;
    //To get our Text
    //public GameObject scoreText;
    Text max;
    Text current;
    //As soon as the Thread awakes, load the Text
    void Awake()
    {
        max = GetComponent<Text>();
        max.text = manager.maxScore.ToString();
    }
    void Update()
    {
        current = GetComponent<Text>();
        current.text = manager.currentScore.ToString();
    }
}

显示当前分数(获得的分数)的文本脚本:

关于可能的分数:

我不明白为什么在显示 Winscreen 后两个值都迅速下降。

感谢您的帮助!

两个文本对象上的 WinScreenScore 运行s。 在 Update 方法中,您获取 Text 组件并将其设置为当前分数。所以对于你的最高分数,这正在发生。唤醒者获取文本组件并将文本设置为来自游戏管理器的最高分数。然后在更新中,将 THE SAME 文本组件设置为当前乐谱。基本上,您使用相同的文本组件填充 max 和 current 。解决方案:创建 public 对包含文本组件的游戏对象的引用,然后获取两个单独的文本。该脚本不需要在两个对象上 运行。

代码示例:

public GameManager manager;
public GameObject max, achieved;
Text currentText, maxText;
void Start()
{
    currentText = achieved.GetComponent<Text>();
    maxText = max.GetComponent<Text>();
    maxText.text = manager.maxScore.ToString();
}
//Since the current Score needs to be updated
void FixedUpdate()
{
    currentText.text = manager.currentScore.ToString();
}

现在您可以独立设置电流和最大值。在 Update 中使用 GetComponent 也是一个坏主意,因为它是一项繁重的操作。尽量只在 运行 一次的地方使用。