我正在尝试为我的游戏创建一个不活动计时器,除了一部分以外,我似乎可以正常运行。进行预计数后,我将实例化一个预制计时器对话框,然后在协程中尝试更新位于该预制件中的文本字段“ timerTextField”,但未更新。

我确实在'timerTextField.text = s.ToString();'之后的编辑器和debug.log中分配了所有适当的变量。线正确倒数。我有什么问题?

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

public class GameManager : MonoBehaviour
{
    // Create Singleton
    public static GameManager gameManagerInstance = null;

    // Set Default Background Color
    public Color defaultColor;

    // Restart variables
    private Vector3 prevMousePosition;

    [SerializeField]
    private Text timerTextField;

    public Object startingScene;
    public GameObject timeOutWarningDialog;
    public float countdownLength;
    public float countdownDelay;

    private float seconds;
    private Scene currentScene;
    private GameObject gameManager;
    private GameObject canvas;
    private GameObject timerInstance;

    void Awake()
    {
        if (gameManagerInstance == null)
            gameManagerInstance = this;
        else if (gameManagerInstance != null)
            Destroy(gameObject);
        DontDestroyOnLoad(gameObject);
        gameManager = GameObject.FindGameObjectWithTag("GameManager");
    }

    void Start()
    {
        prevMousePosition = Input.mousePosition;
        currentScene = SceneManager.GetActiveScene();
    }

    void Update()
    {
        if (Input.anyKeyDown || Input.mousePosition != prevMousePosition)
        {
            currentScene = SceneManager.GetActiveScene();

            if (currentScene.name != startingScene.name)
            {
                StartPreCountTimer();
                if (timeOutWarningDialog != null)
                {
                    timeOutWarningDialog.SetActive(false);
                }
            }
        }
        prevMousePosition = Input.mousePosition;
    }

    // GAME TIMER

    public void StartPreCountTimer()
    {
        // Debug.Log("Pre Count Started");
        CancelInvoke();
        if (GameObject.FindGameObjectWithTag("Timer") == null)
            Invoke("ShowRestartWarning", countdownDelay);
    }

    void ShowRestartWarning()
    {
        canvas = GameObject.FindGameObjectWithTag("Canvas");
        timerInstance = Instantiate(timeOutWarningDialog);
        timerInstance.transform.SetParent(canvas.transform, false);
        timerInstance.SetActive(true);

        if (timerInstance.activeSelf == true)
            StartTimer(countdownLength);
    }

    public void StartTimer(float seconds)
    {
        StartCoroutine("RunTimer", seconds);
    }

    IEnumerator RunTimer(float seconds)
    {
        float s = seconds;
        // Debug.Log("Restart Countdown Started from: " + s);
        while (s > 0)
        {
            if (timerTextField != null)
            {
                timerTextField.text = s.ToString();
                Debug.Log(s);
            }
            yield return new WaitForSeconds(1);
            s -= 1;
        }

        if (s == 0)
        {
            RestartGame();
        }
    }

    void RestartGame()
    {
        SceneManager.LoadScene(startingScene.name);
        // Debug.Log("Game Restarted");
    }
}

最佳答案

您的问题来自timerTextField参考。

似乎您将位于预制件(我认为是timeOutWarningDialog)中的Text组件分配给了Inspector中的timerTextField字段。
因此,在实例化一个新的预制计时器对话框之后,当您要更改timerTextField.text值时,更改的是预制件中的Text组件,而不是实例化的对象之一。

您进行了检查,但是选择了预制中包含Text组件的对象(这是Unity的Project选项卡):

c# - 倒数文字栏位在倒数期间未更新-LMLPHP
(这也说明了开始时的怪异值:停止游戏时达到的先前值)

为了使脚本正常工作,您只需要使用以下示例引用实例化预制件的新Text组件:

void ShowRestartWarning()
{
    timerInstance = Instantiate(timeOutWarningDialog);
    timerInstance.transform.SetParent(transform, false);
    timerInstance.SetActive(true);
    timerTextField = timerInstance.GetComponentInChildren<Text>(); // NEW LINE

    if(timerInstance.activeSelf == true)
        StartTimer(countdownLength);
}


希望这可以帮助,

关于c# - 倒数文字栏位在倒数期间未更新,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44532201/

10-12 01:24