There is my code to count enemies then send Debug log 0000 and it not working why?
public class WIN : MonoBehaviour
{
private int enemiesLeft;
void Start()
{
StartCoroutine(win1());
}
void Update()
{
GameObject[] enemies = GameObject.FindGameObjectsWithTag("enemy");
enemiesLeft = enemies.Length;
}
void win()
{
if (enemiesLeft <= 0)
{
Debug.Log("0000");
CancelInvoke("win");
}
}
IEnumerator win1()
{
yield return new WaitForSeconds(1);
InvokeRepeating("win", 0, 0);
}
}
"Invokes the method methodName in time seconds, then repeatedly every repeatRate seconds.
Note : This does not work if you set the time scale to 0."
So that’s maybe that.
However i find it disturbing that you are using update, coroutine and invokerepeating for this purpose.
A cleaner approach would probably save you from further troubles.
What does the error log say? That’s usually the first clue.
From first glance, you don’t seem to be assigning the ‘enemiesLeft’ variable anywhere initially. The Start() method will run prior to the Update() call, and so you’re starting the IEnumerator before you’ve ever initialized that int.
Now, that delay on the IEnumerator might let the Update() method populate the int before the coroutine tries to access it, that I’m not sure about.
The whole way you’re going about this is… curious. You’re starting a coroutine seemingly just to delay the calling of your Win() method. But then InvokeRepeating already has a delay timer built into it. You aren’t repeating the call, so why use InvokeRepeating?
Just throw ‘if (enemiesLeft <= 0) { Debug.Log(“0000”); }’ into the end of your Update() method and you should be fine, after you initialize your number of enemiesLeft somewhere.