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

时间:2017-04-23 06:38:09

标签: c# unity3d timer countdown

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

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;

    //  }
    //}
}

1 个答案:

答案 0 :(得分:1)

创建文本组件时,默认VerticalOverflow值为截断

所以,当你这样做时:

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

&#34; \ n&#34; Mathf.RoundToInt (timeLeft);写入&#34;剩余时间:&#34;。执行此操作时,文本将不适合TextBox。

您有三种选择:

1 。删除&#34; \ n&#34;

2 。将文字VerticalOverflow设置为截断。

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

timerText.verticalOverflow = VerticalWrapMode.Overflow;

3 。将文字字体从 14 减少到约 13 或更低的数字。

如果您不想如此修改Text,则应使用#2

修改

确保脚本未附加到多个GameObjects。

选择脚本,转到资产 - &gt; 在场景中查找引用,然后从其他对象中删除重复的脚本。