Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 AmazingDeveloper2 · Jan 03, 2015 at 12:50 PM · coroutine

Two coroutinew doesn't works together

In my game I have two coroutines, but when I start simulation, Unity freezes.

Edited. Here is the full code

     public GameObject[] timeImages;
     public GameObject[] time00X0;
     public GameObject[] time000X;
     bool isTimeEnabled;
 
     int timeValue00X0;
     int timeValue000X;
 
 
     void Start ()
     {
         timeValue00X0 = Random.Range (4, 5);
         timeValue000X = 0;
 
         time00X0 [timeValue00X0].SetActive (true);
         time000X [timeValue000X].SetActive (true);
 
         StartCoroutine ("Time000X");
         StartCoroutine ("Time00X0");
     }
 
     IEnumerator Time000X()
     {
         while (true)
         {
             isTimeEnabled = !isTimeEnabled;
             foreach(GameObject i in timeImages)
                 i.SetActive (isTimeEnabled);
 
             time000X[timeValue000X].SetActive(true);
             yield return new WaitForSeconds(0.5f);
             time000X[timeValue000X++].SetActive(false);
 
             if(timeValue000X == 10)
                 timeValue000X = 0;
         }
     }
 
     IEnumerator Time00X0()
     {
         while(true)
         {
             if(timeValue000X == 0)
             {
                 time00X0[timeValue00X0].SetActive(true);
                 yield return new WaitForSeconds(0.5f);
                 time00X0[timeValue00X0++].SetActive(false);
             }
 
             if(timeValue00X0 == 3)
                 timeValue00X0 = 0;
         }
     }

Can't understand, what's wrong

Comment
Add comment · Show 2
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 Scribe · Jan 03, 2015 at 12:59 PM 1
Share

2 basic coroutines work fine for me, what code is in your IEnumerator methods, does the following work for you:

 void Start(){
     StartCoroutine("Time000X");
     StartCoroutine("Time00X0");
     Debug.Log("here");
 }
 
 IEnumerator Time000X(){
     yield return new WaitForSeconds(3);
     Debug.Log(Time.time);
 }
 
 IEnumerator Time00X0(){
     yield return new WaitForSeconds(2);
     Debug.Log(Time.time);
 }
avatar image Scribe · Jan 04, 2015 at 12:08 AM 0
Share

Glad I could help! Done :)

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Scribe · Jan 03, 2015 at 07:16 PM

your problem is that if timeValue000X doesn't equal 0, then you will never reach a yield statement in IEnumerator Time00X0 and therefore your frame will never progress and timeValue000X will never change and so it goes on forever. You should add yield return null; to the end of the second IEnumerator so it will progress without doing anything even if timeValue000X is not 0

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

27 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

Related Questions

Coroutine couldn't be started becuase inactive 0 Answers

coroutine spamming webserver which then stops replying 2 Answers

Yield WaitForSeconds Not Working. Coroutine. 1 Answer

Coroutine Changed local variable behaviour in lambda, some confused 0 Answers

How to wait for coroutine's end from another class 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