Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 倒计时期间未更新倒计时文本字段_C#_Unity3d_Timer - Fatal编程技术网

C# 倒计时期间未更新倒计时文本字段

C# 倒计时期间未更新倒计时文本字段,c#,unity3d,timer,C#,Unity3d,Timer,我正在尝试为我的游戏创建一个不活动计时器,我似乎所有的东西都在工作,除了一个部分。在预计数之后,我正在实例化一个预置计时器对话框,然后在我的协同程序中,我试图更新位于该预置中的文本字段“timerTextField”,但它没有更新 在编辑器和debug.log中,我确实在'timerTextField.text=s.ToString();'之后分配了所有适当的变量该行正在正确倒计时。我怎么了 using UnityEngine; using UnityEngine.UI; using Syste

我正在尝试为我的游戏创建一个不活动计时器,我似乎所有的东西都在工作,除了一个部分。在预计数之后,我正在实例化一个预置计时器对话框,然后在我的协同程序中,我试图更新位于该预置中的文本字段“timerTextField”,但它没有更新

在编辑器和debug.log中,我确实在'timerTextField.text=s.ToString();'之后分配了所有适当的变量该行正在正确倒计时。我怎么了

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
文本组件分配给Inspector内的
timerTextField
字段。
因此,在实例化一个新的预置计时器对话框后,当您想要更改
timerTextField.text
值时,所更改的是预置中的text组件,而不是实例化对象中的组件

选中此选项,但选择包含预置中的文本组件的对象(在Unity的项目选项卡中):


(这也解释了你在开始时的奇怪值:你停止游戏时达到的先前值)

要使脚本正常工作,只需使用以下内容引用实例化预置的新文本组件:

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);
}
void showrestwarning()
{
timerInstance=实例化(timeOutWarningDialog);
timerInstance.transform.SetParent(transform,false);
timerInstance.SetActive(真);
timerTextField=timerInstance.GetComponentChildren();//新行
if(timerInstance.activeSelf==true)
StartTimer(倒计时长度);
}

希望这有帮助,

你确定吗?注释掉您的其他代码,然后尝试在
Start
函数中修改
timerTextField
,并查看它是否更改:
void Start(){timerTextField.text=“Hello”}
@程序员,刚刚尝试过。我在实例化对话框之后就把它放好了,它确实可以工作(Hello显示)。但它并没有在IEnumerator中更新实际的倒计时。为什么会这样呢?事实上@Programmer,我只是试了几次,但还是有问题。当对话开始时,它从1到15之间的一个随机数开始(顺便说一句,15是我的倒计时时间),并在倒计时期间保持不变。2个问题:1.我在第一次评论中说的话有用吗?2.是您的
Debug.LogRunTimer
函数中的code>正确显示倒计时数字?很高兴它有帮助:首先,使用预制件可能很棘手(尤其是嵌套的预制件),但一旦掌握了窍门,它就会非常有用;)