Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by DoubleIsLoveDoubleIsLife · Jan 25, 2016 at 11:15 AM · coroutineyield waitforseconds

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.

Comment
Add comment · Show 28
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DoubleIsLoveDoubleIsLife · Jan 25, 2016 at 11:26 AM 0
Share

I'm calling it from the start function. Still doesn't work btw.

avatar image ShadyProductions DoubleIsLoveDoubleIsLife · Jan 25, 2016 at 11:29 AM -1
Share

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).

avatar image DoubleIsLoveDoubleIsLife · Jan 25, 2016 at 11:32 AM 0
Share

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");
     }
avatar image ShadyProductions DoubleIsLoveDoubleIsLife · Jan 25, 2016 at 11:36 AM 0
Share

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

avatar image DoubleIsLoveDoubleIsLife · Jan 25, 2016 at 11:41 AM 0
Share

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.

avatar image ShadyProductions DoubleIsLoveDoubleIsLife · Jan 25, 2016 at 11:53 AM 0
Share

Isn't that the point that it calls once, because its in the start method?:P

avatar image DoubleIsLoveDoubleIsLife DoubleIsLoveDoubleIsLife · Jan 25, 2016 at 12:00 PM 0
Share

I meant it only processes once, as in, the debug log says "1" and then "2", and doesn't repeat.

avatar image Bonfire-Boy · Jan 25, 2016 at 12:09 PM 1
Share

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?

avatar image Bonfire-Boy · Jan 25, 2016 at 01:52 PM 4
Share

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.

Show more comments

3 Replies

· Add your reply
  • Sort: 
avatar image
6

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by ZachAttack9280 · Apr 11, 2018 at 02:38 PM

StartCoroutine(ProcessEntities());

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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);
 }

}

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

44 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Coroutines Didn't Work After Changing Scene 1 Answer

C# yield waitforseconds 1 Answer

How to pause a Coroutine? 5 Answers

Reset WaitForSeconds Coroutine 1 Answer

Coroutine "While" Setup 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges