Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Something more about CoRoutines

Discussion in 'Scripting' started by Leggy7, Aug 4, 2015.

  1. Leggy7

    Leggy7

    Joined:
    Feb 18, 2014
    Posts:
    21
    Hello everyone,
    I was trying to understand some unexpected behavior about CoRoutines.

    The situation is the following:

    There are three public gameobject instantiated with the same prefab, this way

    Code (csharp):
    1. go1 = GameObject.Instantiate (prefab);
    2. go2 = GameObject.Instantiate (prefab);
    3. go3 = GameObject.Instantiate (prefab);
    Then I call three times a CoRoutine to destroy those gameobjects in three different moments but from the same method, this way

    Code (csharp):
    1. if(cond 1){
    2.     StartCoroutine (WaitTotObj (go1));
    3.     return;
    4. }
    5. if(cond 2){
    6.     StartCoroutine (WaitTotObj (go2));
    7.     return;
    8. }
    9. if(cond 3){
    10.     StartCoroutine (WaitTotObj (go3));
    11.     return;
    12. }
    WaitToObj is something like this

    Code (csharp):
    1. public IEnumerator WaitTotObj(UnityEngine.Object obj){
    2.         yield return new WaitForSeconds (((GameObject)obj).GetComponent<ParticleSystem> ().startLifetime+1);
    3.         Destroy (obj);
    4. }
    What happens next is that only one of the three gameObject gets destroyed.

    Can anyone explain me this behavior and, in the case, just tell me how to solve this problem?
     
    Last edited: Aug 4, 2015
  2. martinmr

    martinmr

    Joined:
    Mar 25, 2015
    Posts:
    325
    Are your if conditions in any kind of loop, for, foreach ? because "break;" breaks the loop and skips rest of the code under it.
     
  3. Leggy7

    Leggy7

    Joined:
    Feb 18, 2014
    Posts:
    21
    Sorry, They are return commands. I updated main post.
    The functions is called to trigger particle effects in different moments, there are no loops in it.

    The thing I really can't explain is that if I change the conditions adding code like this

    Code (csharp):
    1. if(cond 3){
    2.     StartCoroutine (WaitTotObj (go3));
    3.     go3 = null;
    4.     return;
    5. }
    all the gameobjects get destroyed correctly. But I can't understand why.

    I forgot to say that cond*number* are like

    Code (csharp):
    1.  go*number* != null
     
  4. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    What is the MonoBehaviour that is starting the coroutine on? Coroutines will stop executing if the object they are called on is destroyed or set to inactive.
     
  5. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,384
    Code (csharp):
    1. StartCoroutine(WaitTotObj (go1));
    What is this? Where do you get a reference to 'WaitTotObj'?

    And please, post the actual script, theres spelling errors, questions and missing code that could be related all over the place in here.
     
  6. Leggy7

    Leggy7

    Joined:
    Feb 18, 2014
    Posts:
    21
    I think I had some response for this problem.
    For how was structured my code if I didn't set the object to null, after calling the coroutine, further calls would have entered the first condition. The effect played anyway because I forgot to uncheck play on awake on the particle systems being instantiated.

    I want to thank you so much for your precious time
     
  7. Leggy7

    Leggy7

    Joined:
    Feb 18, 2014
    Posts:
    21
    The WaitToObj function is in the same script. It gets called right away.
    My mistake was on the hierarchy conditions, since there wasn't else if clause to avoid entering the others.

    And, sorry, I simplified the code to avoid confusion. Thank you for your time too!