简体   繁体   中英

How do I update a counter using coroutine in Unity3d?

I'm trying to make a basic 1,2,3 go countdown but when i pass countDown to countTxt field there no changes even with for loop. I can see in Unity inspector how the countdown works and goes from 3 to 0 but not on my text field.

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class CountDown : MonoBehaviour {
public int countDown;
public Text countTxt;
public int countMax ;

void Update () {
    StartCoroutine (GetReady ());
}

IEnumerator GetReady () {
        for (countDown = countMax; countDown > 0;countDown--)
        {
            countTxt.text = countDown.ToString();
            yield return new WaitForSeconds(1);
        }
    }
}

你正在调用Update()里面的协程,所以它一直被调用....它应该在Start()中调用

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM