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 folta20 · Nov 04, 2014 at 04:28 AM · c#coroutine

how to make a coroutine finish first before other coroutine start

Hi im a newbie in unity and c#..

I have two script file in the same scene,

1 coroutine in file versionchecker.cs to get a version number data from my web server

 public string versionURL = "http://localhost/check.php";
     
     IEnumerator GetVersion()
     {
         WWW vs_get = new WWW(versionURL);
         yield return vs_get;
         
         if (vs_get.error != null)
         {
             connection = 1;
         }
         else
         {
             currentVersion = vs_get.text;
             bundleVersion = PlayerSettings.bundleVersion;
             connection = 0;
         }
     }


But in another file in beginingscreen.cs, i have a coroutine for a begining screen..

 void Start () {
         if(!isExit)
             StartCoroutine (BeginningAnimation ());
         else
             StartCoroutine (EndAnimation ());
     }
     
     IEnumerator BeginningAnimation()
     {
         fade.FadeIn (1.5f);
         yield return new WaitForSeconds (2);
         fade.FadeOut (1);
         yield return new WaitForSeconds (0.9f);
         Application.LoadLevel (LevelToLoad);
     }
 
     IEnumerator EndAnimation()
     {
         yield return new WaitForSeconds (0.5f);
         fade.FadeOut (1);
         yield return new WaitForSeconds (1);
         Application.Quit ();
     }


this script i place it in the same scene of my game.. but sometimes the coroutine for begining screen finish first before the coroutine for get version because the get version need a connection to webserver, and sometime the web server is lagging..

so how can i make the get version coroutine finish first and after that begining screen can start..

Comment
Add comment
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

1 Reply

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

Answer by b1gry4n · Nov 04, 2014 at 04:39 AM

Since you have 2 scripts a solution could be to have each script manage their own coroutine and turning the coroutine off is controlled via accessing a function.

http://docs.unity3d.com/ScriptReference/MonoBehaviour.StopCoroutine.html

Note that in order for stopcoroutine to work you must start the coroutine using a string name of the IEnumerator. EX:

 StartCoroutine("MyCoroutine");
 
 IEnumerator MyCoroutine(){
 
 }

An example of how to do what i have just described...

Script A

     private bool coroutineRunning = false;
     
     void Start(){
         coroutineRunning = true;
         StartCoroutine("MyCoroutine");
         Debug.Log("Coroutine Started");
     }
     
     public void StopTheCoroutine(){
         if(coroutineRunning){
             coroutineRunning  = false;
             Debug.Log("Stopping the coroutine");
             StopCoroutine("MyCoroutine");
         }
     }
     
     IEnumerator MyCoroutine(){
         yield return new WaitForSeconds(10.0f);
         Debug.Log("Coroutine has elapsed");
         coroutineRunning = false;
     }

Script B

     void StopTheCoroutineOnScriptA(){
         scriptA.StopTheCoroutine();
            Debug.Log("Script B has stopped Script A's coroutine");
     }

The above will stop the coroutine running dead in its tracks. If you are trying to wait for the coroutine to finish before starting another:

http://answers.unity3d.com/questions/126783/can-i-check-if-a-coroutine-is-running.html

By utilizing a method the user Tetrad has described in another question, you can use "while" loops to wait for the other coroutine to end. An example from that question is posted below as well as a link

http://answers.unity3d.com/questions/19562/c-while-loop-question.html

Tetrad ~

     IEnumerator DragObject( stuff )
     {
         // setup stuff
         while( Input.GetMouseButton( 0 ) )
         {
             // update position stuff
             yield return null; // returning null is faster than returning 0
         }
         //done with dragging stuff
     }
Comment
Add comment · Show 1 · 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 folta20 · Nov 04, 2014 at 04:45 AM 0
Share

oh thx sir.. let me try ur example.. thx before :)

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Using Coroutines to increase Float values C# 3 Answers

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

Unexpected Co-Routine Behavior 1 Answer

A node in a childnode? 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