计时器在游戏过程中保持为 0

Timer stays at 0 during gameplay

screenshot我目前正在为我的 unity 游戏添加一个倒数计时器,但是实际数字在游戏过程中并没有下降,而是在 "Time Left" 的检查面板中逐渐下降。有人知道为什么吗?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class trackingclicks : MonoBehaviour {

    //static variable added to count users clicks
    //public static int totalclicks=0;
    //"mouseclick" keycode variable to look for mouse click
    public KeyCode mouseclick;
    public float timeLeft;
    public Text timerText;


    //public Transform scoreObj;

    // Use this for initialization
    void Start () {
        timerText.text = "Time Left:\n" + Mathf.RoundToInt (timeLeft);

    }

    void FixedUpdate () {
        timeLeft -= Time.deltaTime;
        if (timeLeft < 0) {
            timeLeft = 0;
        }

        timerText.text = "Time Left:\n" + Mathf.RoundToInt (timeLeft);
    }

    void UpdateText () {
        timerText.text = "Time Left:\n" + Mathf.RoundToInt (timeLeft);

    }
    // Update is called once per frame
    //void Update () {
    //checks the change in time, aka how much time has passed- bonus time starts at 90
        //clickcontrol.timeBonus -= Time.deltaTime;

        //if (clickcontrol.remainItems == 0) 
        //{
        //  clickcontrol.totalScore += (70 + (Mathf.RoundToInt(clickcontrol.timeBonus)));
            //scoreObj.GetComponent<TextMesh>().text = "Score : " + clickcontrol.totalScore;
            //clickcontrol.remainItems = -1;

        //}
    //Check for mouse click
    //if (Input.GetKeyDown (mouseclick)) 
        //{
        //  totalclicks += 1;

    //  }

    //  if (totalclicks >= 5) 
    //  {
    //      Debug.Log ("FAIL!!!");
        //  totalclicks = 0;

    //  }
    //}
}

创建文本组件时,它的默认 VerticalOverflow 值为 Truncate

所以,当你这样做时:

timerText.text = "Time Left:\n" + Mathf.RoundToInt (timeLeft);

"\n"会使Mathf.RoundToInt (timeLeft);写在"Time Left:"下。当您这样做时,文本将不适合 TextBox。

你有三个选择:

1.去掉"\n".

2。将文本的 VerticalOverflow 设置为截断。

这就是您需要做的所有事情:

timerText.verticalOverflow = VerticalWrapMode.Overflow;

3。将文本字体从 14 减小到大约 13 或更低数.

如果你不想修改你的 Text 太多,那么应该使用 #2

编辑:

确保脚本未附加到多个游戏对象。

Select 脚本,转到 Assets --> Find References in Scene 然后从其他文件中删除重复的脚本对象。