1
\$\begingroup\$

I have a function for a timer using waitforseconds but instead of waiting for how long I want to, it just skips it.

A ui text should become "foo" then 5 seconds later become "bar" but it turns to bar as soon as it starts.

Code:

void Update () {
     text1.text = "foo";
     wait (5);
     text1.text = "bar";
 }

 IEnumerator wait(int time)
 {
     yield return new WaitForSeconds(time);
 }
\$\endgroup\$
  • \$\begingroup\$ I don't know syntax off top of my head, but throw the "bar" line into the Wait method after the wait line \$\endgroup\$ – Evorlor Jul 27 '15 at 20:58
2
\$\begingroup\$

You have to use StartCoroutine (probably not in the Update) in order to start its execution. Otherwise it's only a method call:

void Start()
{
    StartCoroutine(wait(5));
}
| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

You have to call you coroutine by writing "StartCoroutine( wait )". You should put something before and after the yield command or else it is skipped. Try creating a boolean and disabling or enabling it before and after the yield, then you could adjust your code to check for that boolean and then run.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.