You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity答题游戏计时器到达上限后无法重新计数问题排查

问题及解决:Unity答题游戏计时器仅第一题正常,重置后不再倒计时

问题描述

开发一款每道题配计时器的Unity答题游戏,脚本实现从20秒倒计时至0,计时结束后显示正确答案、播放音效并切换下一题。但游戏运行时仅第一题正常,计时器重置为20后不再继续倒计时,相关C#脚本如下:

public class TimerSettings : MonoBehaviour
{
    [Header("Component")]
    public TextMeshProUGUI timerText;


    [Header("Timer Settings")]
    public float currentTime; //this is set to 20

    [Header("Limit Settings")]
    public bool hasLimit;
    public float timerLimit; //this is set to 0

    public GameObject answerAbackblue;
    public GameObject answerAbackgreen;
    public GameObject answerBbackblue;
    public GameObject answerBbackgreen;
    public GameObject answerCbackblue;
    public GameObject answerCbackgreen;
    public AudioSource incorrectSFX;
    public GameObject answerA;
    public GameObject answerB;
    public GameObject answerC;




    private void Update()
    {
        
        
            currentTime -= Time.deltaTime;
            

            if (currentTime <= 5)
            {
                timerText.color = Color.red;
            }

            if (hasLimit && (currentTime <= timerLimit))
            {
                currentTime = 0f;
                SetTimerText();
                enabled = false;
                if (QuestionGenerate.actualAnswer == "A")
                {
                    answerAbackgreen.SetActive(true);
                    answerAbackblue.SetActive(false);
                    incorrectSFX.Play();
                }
                else if (QuestionGenerate.actualAnswer == "B")
                {
                    answerBbackgreen.SetActive(true);
                    answerBbackblue.SetActive(false);
                    incorrectSFX.Play();
                }
                else
                {
                    answerCbackgreen.SetActive(true);
                    answerCbackblue.SetActive(false);
                    incorrectSFX.Play();
                }
            StartCoroutine(NewQuestion());

                

            }

            SetTimerText();
        


        
    }

    private void SetTimerText()
    {
        timerText.text = currentTime.ToString("0.0");
    }

    

    IEnumerator NewQuestion()
    {
        yield return new WaitForSeconds(1);
        answerAbackgreen.SetActive(false);
        answerBbackgreen.SetActive(false);
        answerCbackgreen.SetActive(false);
        answerAbackblue.SetActive(false);
        answerBbackblue.SetActive(false);
        answerCbackblue.SetActive(false);
        answerA.GetComponent<Button>().enabled = true;
        answerB.GetComponent<Button>().enabled = true;
        answerC.GetComponent<Button>().enabled = true;
        QuestionGenerate.displayingQuestion = false;
        QuestionGenerate.score++;
        timerText.color = Color.white;
        currentTime = 20f;
        timerText.text = currentTime.ToString("0.0");
    }

}

问题原因

核心问题出在计时结束逻辑里的enabled = false;语句:

  • 第一次计时结束后,TimerSettings组件被禁用,导致Update()方法不再被Unity调用
  • 尽管NewQuestion()协程里重置了currentTime = 20f,但组件已禁用,Update()无法执行倒计时逻辑,计时器因此停在20不动

解决方法

有两种可行的修复方式:

方式一:移除enabled = false;语句

直接删除计时结束分支里的enabled = false;,让组件始终保持启用状态,Update()会持续执行,重置时间后即可继续倒计时。修改后的代码片段:

if (hasLimit && (currentTime <= timerLimit))
{
    currentTime = 0f;
    SetTimerText();
    // 移除 enabled = false;
    if (QuestionGenerate.actualAnswer == "A")
    {
        answerAbackgreen.SetActive(true);
        answerAbackblue.SetActive(false);
        incorrectSFX.Play();
    }
    // 其余逻辑保持不变
    StartCoroutine(NewQuestion());
}

方式二:在NewQuestion()协程末尾重新启用组件

如果需要暂时禁用组件(比如防止计时结束后继续执行Update逻辑),可以在协程最后添加启用组件的代码:

IEnumerator NewQuestion()
{
    yield return new WaitForSeconds(1);
    // 其余重置逻辑保持不变
    currentTime = 20f;
    timerText.text = currentTime.ToString("0.0");
    // 重新启用组件
    enabled = true;
}

推荐使用方式一,逻辑更简洁,避免组件启用禁用带来的额外状态管理问题。

内容的提问来源于stack exchange,提问作者hpethera

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.27 04:13:23