- Home /
Coroutine Will NOT Start
Hey guys, I've got some weird problem that's really doing my head in. Am I missing something? Here is a coroutine >
IEnumerator ProcessEntities()
{
Debug.Log("1");
while (true)
{
Debug.Log("2");
allies.CopyTo(temp);
foreach (Entity a in temp)
{
if(a.GO.tag != "Player" && a.GO.GetComponent<AI>() == null)
{
allies.Remove(a);
}
}
enemies.CopyTo(temp);
foreach (Entity a in temp)
{
if (a.GO.tag != "Player" && a.GO.GetComponent<AI>() == null)
{
enemies.Remove(a);
}
}
yield return new WaitForSeconds(entityProcessRate);
}
}
Which I am calling from the same scripts start method >
StartCoroutine("ProcessEntities");
For some reason, the coroutine will not get called, at all. None of the Debug messages pop up. When I Start the coroutine through an update function it is called once per frame. The update rate for the coroutine should be 0.2 seconds.
I'm calling it from the start function. Still doesn't work btw.
can I see your start method?
Also try putting yield return null;
at the end of the coroutine (after the end of the while loop).
Here it is >
void Start () {
if (Application.loadedLevel != null)
{
player = GameObject.FindWithTag("Player");
defaultFixedDeltaTime = Time.fixedDeltaTime;
AddAlly(player);
Cursor.lockState = CursorLock$$anonymous$$ode.Locked;
Cursor.visible = false;
objectPool.bullet = Instantiate(objectPool.bullet);
objectPool.bloodEnter = Instantiate(objectPool.bloodEnter);
objectPool.gunShotParticle = Instantiate(objectPool.gunShotParticle);
for (int i = 0; i < objectPool.bullets.Count; i++)
{
objectPool.bullets[i] = Instantiate(objectPool.bullet);
}
for (int i = 0; i < objectPool.bloodEnters.Count; i++)
{
objectPool.bloodEnters[i] = Instantiate(objectPool.bloodEnter);
}
for (int i = 0; i < objectPool.gunShots.Count; i++)
{
objectPool.gunShots[i] = Instantiate(objectPool.gunShotParticle);
}
LoadOptions();
SetOptions();
Invoke("DelayedStart", 0.05f);
}
StartCoroutine("ProcessEntities");
}
Could be that it never gets past your DelayedStart method, that it never gets to call the coroutine. Put some debugs at the end of your Invoke. $$anonymous$$ight help :P
Okay, now it gets even more confusing, the delayed start method works >
public void DelayedStart()
{
Debug.Log("NO, $$anonymous$$E$$anonymous$$ES!");
for (int i = 0; i < objectPool.bullets.Count; i++)
{
tempGO = objectPool.bullets[i];
tempGO.SetActive(false);
}
for (int i = 0; i < objectPool.bloodEnters.Count; i++)
{
tempGO = objectPool.bloodEnters[i];
tempGO.SetActive(false);
}
for (int i = 0; i < objectPool.gunShots.Count; i++)
{
tempGO = objectPool.gunShots[i];
tempGO.SetActive(false);
}
Debug.Log("WOW, $$anonymous$$E$$anonymous$$ES!");
}
All of the object pool stuff gets instantiated, but the two debug logs don't get called.
Isn't that the point that it calls once, because its in the start method?:P
I meant it only processes once, as in, the debug log says "1" and then "2", and doesn't repeat.
Are you sure that the GameObject this stuff is on isn't being deactivated somehow?
Also, it looks to me like you're relying on ti$$anonymous$$g here. I think I'd be looking to start the ProcessEntities coroutine when the objects have been instantiated. Why do you kick it off before DelayedStart()
?
Finally (and this may just be a point of curiosity) what is the point of the if (Application.loadedLevel != null)
in the Start function?
Sorry, I meant the adapted coroutine-based code. I'm here trying to help you get your coroutine working. Seems to me you're giving up very easily. Sometimes one gets a blind spot and a bug is hard track down, it happens to all of us. Lots of (descriptive) logging almost always leads one in the right direction. Occasionally I've ended up with every other line being a Debug.Log, and lots of $$anonymous$$onoBehaviour methods (OnEnable, OnDisable, and so on) that are only there to log that they've been called, before cottoning on to what I've done wrong.
Just to clarify about the code - you originally said the CR doesn't start. Then after making changes following other people's comments on here you said it started but didn't loop. But you've not shown us those changes. By the way, what you've been describing is consistent with calling ProcessEntities()
ins$$anonymous$$d of StartCoroutine(ProcessEntities())
(the yield return would end the function in that case, like a regular return
statement). If you showed us the code, we might be able to spot an error of that sort for you.
Answer by MarkKreitler · Jul 08, 2019 at 05:27 AM
I came to this thread with the same problem. My solution won't help you (even if I wasn't 3 years too late), but it might help someone else.
My IDE's autocomplete caused my coroutine's declaration to be this:
private IEnumerable myCoroutine() {...}
instead of IEnumerator
If you execute the coroutine using StartCoroutine("myCoroutine");
you won't get an error -- the coroutine just doesn't execute.
Answer by shyxiaolong · Nov 06, 2021 at 10:57 AM
i am dont have enogh information. but i think this way can solve your problem or problem another man. Yoy can create variable IEnumerator currentCoroutine and pass ur coroutine
IEnumerator current = null;
IEnumerator MyCoroutine(){
}
private void Awake(){
if(current == null){
current = MyCoroutine();
}
}
private void Start(){
if(current != null){
StopCoroutine(current) //if neccessery
StartCoroutine(current);
}
}