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;
}
}
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?
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.
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
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.
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: