Search Unity

  1. Going to #UniteBerlin? Bring a buddy & get 25% off the 2nd ticket! More info here.
    Dismiss Notice
  2. Famed game designer and creator of The Sims, SimCIty, and Spore is looking for a 3D artist to join their team. Join the challenge.
    Dismiss Notice
  3. Unity 2017.4 has arrived! Read about it here.
    Dismiss Notice
  4. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  5. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  6. We've opened a new World Building sub-forum to faciltate all your world building discussions! Drop in and ask any ProBuilder, ProGrids, or PolyBrush questions.
    Dismiss Notice
  7. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

ending old coroutine when new one starts

Discussion in 'Scripting' started by aetztztztzzew, Apr 12, 2018.

  1. aetztztztzzew

    aetztztztzzew

    Joined:
    Apr 12, 2018
    Posts:
    6
    void OnCollisionEnter2D(Collision2D kolizija)
    {
    StartCoroutine( pokupljen(kolizija));
    }


    IEnumerator pokupljen(Collision2D kolizija)
    {


    yield return...

    }

    this is part of a code imade for activating and switching between powerups and it is working fine but i have problem with picking up same powerup before previous coroutine has expired and cant figure out how to make it work.
    i would like to stop first coroutine when next one is activated.
    i tried with yield break and stopcoroutine and it didnt help.
    i would appreciate any help.
    i am first time posting so i apologize if i broke any rulles or didn't put thread in right place
    thank you!
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    3,458
  3. QuinnWinters

    QuinnWinters

    Joined:
    Dec 31, 2013
    Posts:
    266
    Another option is to just use a bool to check if the coroutine is running and if it is stop it before starting another instance of it.
    Code (CSharp):
    1.     bool pokupljenIsRunning = false;
    2.  
    3.     void OnCollisionEnter2D (Collision2D kolizija) {
    4.         if (pokupljenIsRunning) {
    5.             StopCoroutine ("Pokupljen");
    6.         }
    7.         StartCoroutine (Pokupljen (kolizija));
    8.     }
    9.  
    10.     IEnumerator Pokupljen (Collision2D kolizija) {
    11.         pokupljenIsRunning = true;
    12.         //Do whatever
    13.         //If it exits on its own set pokupljenIsRunning to false
    14.     }
     
    Kiwasi and aetztztztzzew like this.
  4. aetztztztzzew

    aetztztztzzew

    Joined:
    Apr 12, 2018
    Posts:
    6
    thank you for replying.
    i tried your code but it doesnt seem to work for me.
    i also tried it by changing
    stopcoroutine("pokupljen") to stopcoroutine (pokupljen(kolizija ))
    and that didnt work either.
    i dont get errors it just doesnt end previous coroutine
     
  5. aetztztztzzew

    aetztztztzzew

    Joined:
    Apr 12, 2018
    Posts:
    6
  6. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    3,458
    Generally speaking, the string version shouldn't be used for start or stopcoroutine.

    Is this script on your player or the object you're picking up?
     
    Kiwasi likes this.
  7. aetztztztzzew

    aetztztztzzew

    Joined:
    Apr 12, 2018
    Posts:
    6
    script is placed on powerup gameobject and oncollisionenter is supposed to happen when player collides with it
     
  8. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    3,458
    Well, that's your problem. Each object has it's own instance of the script on it, which means if I hit powerup 1, it runs, but then powerup 2 has no way of knowing the coroutine is running on the first powerup, so stopping it isn't going to work by simply calling stopcoroutine on the second powerup script. You could make the coroutine variable static, but not sure that is the best way of doing it without knowing more of your intended results of your powerup system.

    Otherwise, you'll need the script to be more central, either on the player or on a manager type script that your powerup script can call.
     
    Kiwasi likes this.
  9. aetztztztzzew

    aetztztztzzew

    Joined:
    Apr 12, 2018
    Posts:
    6

    hi again
    i tried to do what you told me.
    made new script and connected it to player gameobject which never gets destroyed.but it didnt change anything.. do you have any suggestions on what could i check

    Code (CSharp):
    1.  bool pokupljenIsRunning = false;
    2.     void OnCollisionEnter2D (Collision2D kolizija) {
    3.         if (pokupljenIsRunning) {
    4.             StopCoroutine (Pokupljen(kolizija));
    5.         }
    6.         StartCoroutine (Pokupljen (kolizija));
    7.     }
    8.     IEnumerator Pokupljen (Collision2D kolizija) {
    9.         pokupljenIsRunning = true;
    10.         //Do whatever
    11.         //If it exits on its own set pokupljenIsRunning to false
    12.     }
    Code (CSharp):
    1.  bool pokupljenIsRunning = false;
    2. ienumerator old;
    3.     void OnCollisionEnter2D (Collision2D kolizija) {
    4.         if (pokupljenIsRunning) {
    5.             StopCoroutine (old);
    6.         }
    7.         StartCoroutine (Pokupljen (kolizija));
    8.     }
    9.     IEnumerator Pokupljen (Collision2D kolizija) {
    10.      
    11. old=Pokupljen(kolizija);
    12. if(kolizija.gameObject.tag=="powerup")
    13.  { pokupljenIsRunning = true;
    14.        //...
    15.  
    16.          }
     
  10. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    3,458
    Well, you didn't do the code right...
    Code (CSharp):
    1.  
    2. old = StartCoroutine(Pokupljen(kolizija));
    3.  
     
    aetztztztzzew likes this.
  11. QuinnWinters

    QuinnWinters

    Joined:
    Dec 31, 2013
    Posts:
    266
    I actually did not know that. Now that I've researched it I see that the string version has a higher overhead when called. Thanks for mentioning it, that's one more way I can cut down on overhead in my scripts.
     
  12. aetztztztzzew

    aetztztztzzew

    Joined:
    Apr 12, 2018
    Posts:
    6
    that's it, thank you. i really appreciate your help.
    may the codes be with you