Unity C# yeld WaitForSeconds don't work

Hello, i don’t know why my script don’t work, i think that i should see number from time every 2 second, but debug log msg only 1 time, and nothing more. Here is my script:

using UnityEngine;
using System.Collections;

public class Atak1 : MonoBehaviour {
  
    private bool ready = true;
    private int time = 0;

    void Start () {
    }

    void Update () {
    if(ready){
        StartCoroutine(test ());
        }
    }

    IEnumerator test()
    {
        ready = false;
        Debug.Log ("Liczba: " + time);
        time++;
        yield return new WaitForSeconds(2);
        ready = true;
    }
    }

As i said i see only:

Liczba:0
UnityEngine.Debug:Log(Object)

in debug log 1 time and nothing after this.

It works correctly for me. If you use only this script in an empty scene it will work for you as well.
Are you deactivating the component or the game object in any way?

No, i create new empty GameObject, add this script as new Component, click play and nothing. That what i said in 1st post :frowning:

Did you try it in an empty scene?

The output you got is the correct output for the code you wrote. What are you actually trying to do?

EDIT: I misread what your output was, apologies.

It’s not correct, after wait 2 second it should change bool ready to true, and after this Update void should get it and start Coroutine again.

Right click the inspector window with the gameobject with this script attached and change to “debug”. Then you can see if the boolean is changing or not correctly.

It is working correctly. I tested it. There is probably something else in the scene that deactivates the game object or component.

It’s changing only time INT 1x, from 0 to 1

time++;

But code after yield return new WaitForSeconds(2); is like invisible, because in debug mode it show that ready boolean, which should change to ready = true; after 2 seconds are still like before: False

Also, are you actually waiting the 2 seconds for the time to increment again?

It’s actually correct that it will still show false as it almost instantly set to false when it becomes true anyway

Do you have any idea where should i search this error?

#UP Yes, but it’s not change to true after 2 seconds

Ok i fixed that, problem was cuz i has changed Timescale to 0… Thanks all for help

Again, did you try it in an empty scene where you just have a game object with this script?
The answer is no, because I have tested it and it works.

Did you check whether the game object or the component is somehow deactivated?
No, you didn’t.

The cause why the execution doesn’t continue is usually that the game object or the component is deactivated. It would kind of make sense to check that.

Edit: Or the time scale is set to zero :wink:

1 Like

I think you’re confused about how coroutines work. The point of the yield statement is that the function stops executing there, and the next time it’s called, it continues running right where you left it. If you don’t yield in the middle of a loop, they don’t repeat. Unless you have something like:

while(supposedToRun){
       Debug.Log ("Liczba: " + time);
        time++;
        yield return new WaitForSeconds(2);
}

it won’t repeat.

The original code works perfectly fine. It will be started over and over again because that happens in the Update method.