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

Is it wrong to use IEnumerators/Coroutines in place of voids?

Discussion in 'Scripting' started by Caelorum, Mar 25, 2015.

  1. Caelorum

    Caelorum

    Joined:
    Feb 6, 2014
    Posts:
    32
    Just as an example, i use this call, and this script to increase the speed of my ball based on it's location.

    Code (CSharp):
    1. StartCoroutine ("IncreaseSpeed");
    2.  
    3.  
    4. IEnumerator IncreaseSpeed()
    5.     {
    6.  
    7.         ballCheck = GameObject.FindGameObjectWithTag("ball");
    8.         rbc = ballCheck.GetComponent<Rigidbody2D>();
    9.  
    10.         if (ballCheck == null)
    11.         {
    12.             yield return new WaitForSeconds(2);
    13.             StartCoroutine ("IncreaseSpeed");
    14.         }
    15.         if(ballCheck != null)
    16.         {
    17.             if (ballCheck.transform.position.x > 0)
    18.             {
    19.                 if (ballCheck.transform.position.y > 0)
    20.                 {
    21.                     rbc.AddForce (new Vector2(50,50));
    22.                 }
    23.                 if (ballCheck.transform.position.y < 0)
    24.                 {
    25.                     rbc.AddForce (new Vector2(50,-50));
    26.                 }
    27.             }
    28.      
    29.             if(ballCheck.transform.position.x < 0)
    30.             {
    31.                 if(ballCheck.transform.position.y > 0)
    32.                 {
    33.                     rbc.AddForce (new Vector2(-50,50));
    34.                 }
    35.                 if (ballCheck.transform.position.y < 0)
    36.                 {
    37.                     rbc.AddForce (new Vector2 (-50,-50));
    38.                 }
    39.         }
    40.         }
    41.     }
    I was going to use a void at first, but if the ball is null, i need it to wait 2 seconds and then retry the script in order for it to give the ball time to respawn. The only way i can make it wait is with an IEnumerator run through a coroutine. I could use something like
    Code (CSharp):
    1. if(ballCheck == null)
    2. {
    3.          Invoke(Voidthingy,2)
    4. }
    5. if(ballCheck != null)
    6. {
    7. Voidthingy()
    8. }
    But I don't see the reason to when i can just use an IEnumerator instead and include the yield inside.

    Is this a bad habit? I'm starting to get really addicted to IEnumerators thanks to their yield ability. Every now and then i'll just create a coroutine instead of a void so i can take advantage of that.

    I'm not a super advanced coder. I'm just now getting into the heavier stuff like coroutines and thinking up problems for myself, so I might be screwing myself over in the end.
     
  2. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    No problem whatsoever.

    If you need a coroutine, use an IEnumerator. If you need a float, use float. If you need a CharacterController use that.

    void is actually the special case, where you don't need a return value.
     
    Caelorum likes this.
  3. Caelorum

    Caelorum

    Joined:
    Feb 6, 2014
    Posts:
    32
    Awesome thanks. I was just unaware if calling a coroutine had any performance, or coding effect on the end game. If maybe using tons of coroutines could cause any serious problems down the line.
     
  4. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    There is a tiny overhead to coroutines. But unless you are calling thousands in one frame it won't be noticeable.

    And even if you do need thousands, I would profile before assuming the overhead is significant.

    Pre optimisation is evil.
     
    Caelorum likes this.