【问题标题】:Reset timer with a button in Unity在 Unity 中使用按钮重置计时器
【发布时间】:2021-07-15 10:06:07
【问题描述】:

我正在 Unity 中制作一个从 60 倒退到 0(秒)的计时器。当计时器达到零时,它会加载一个新场景(GameOverMenu),但我正在尝试制作一个按钮来再次加载主场景(GameScene)并重新启动计时器,因为计时器在重新加载场景时不会自行重置。添加公共无效时出现错误。有谁知道如何解决这个问题?

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


public class Timer : MonoBehaviour
{
    public Text textArea;
    public string levelToLoad;
    private bool stopTimer;

    // Start is called before the first frame update
    void Start()
    {
        stopTimer = false;
        TimerReset();
    }

    public void TimerReset()
    {
        print("RESET");
        textArea = "0:60";
    }

    // Update is called once per frame
    void Update()
    {
        float time = 60 - Time.time;

        int minutes = Mathf.FloorToInt(time / 60f);
        int seconds = Mathf.FloorToInt(time - minutes * 60);

        string textTime = string.Format("{0:0}:{1:00}", minutes, seconds);

        if(stopTimer == false)
        {
            textArea.text = textTime;
        }

        if ( time <= 0)
        {
            textArea.text = "0:00";
            stopTimer = true;
            SceneManager.LoadScene("GameOverMenu");
        }

    }

}

这是加载“GameScene”的代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class LoadGame : MonoBehaviour
{

    public void changeScene(string sceneName)
    {
        SceneManager.LoadScene("GameScene");
    }

}

【问题讨论】:

  • 什么错误??
  • 错误出现在 "public void TimerReset() { print("RESET"); textArea = "0:60"; }" and "TimerReset();" 行中
  • 有任何错误日志吗?
  • Assets\Timer.cs(23,20):错误 CS0029:无法将类型“字符串”隐式转换为“UnityEngine.UI.Text”
  • textArea.text = "0:60"

标签: c# unity3d user-interface


【解决方案1】:

textArea.text = "0:60";您忘记调用属性文本。

【讨论】:

    猜你喜欢
    • 2016-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-28
    • 1970-01-01
    • 2019-08-09
    • 1970-01-01
    相关资源
    最近更新 更多