簡體   English   中英

在 Unity 中使用按鈕重置計時器

[英]Reset timer with a button in Unity

我正在制作一個在 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");
    }

}

textArea.text = "0:60"; 您忘記調用屬性文本。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM