Nikita Chernykh
Nikita Chernykh

Reputation: 270

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);
        }
    }
}

Upvotes: 1

Views: 2005

Answers (1)

YNK
YNK

Reputation: 869

You are calling the coroutine inside Update() so its getting called all the time....it should be called in Start()

Upvotes: 3

Related Questions