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. Dismiss Notice

Invoke Repeating a certain amount of times

Discussion in 'Scripting' started by BlackMantis, Oct 26, 2010.

  1. BlackMantis

    BlackMantis

    Joined:
    Feb 7, 2010
    Posts:
    1,475
    Best method for repeating something a certain amount of times? I sometimes just use a condition to compare an interger value. But do we have something with a built parameter to count down?:)
     
  2. Ntero

    Ntero

    Joined:
    Apr 29, 2010
    Posts:
    1,436
    You could use a Coroutine with a counter in it's scope.

    i.e.

    Code (csharp):
    1.  
    2. public class MyClass : MonoBehaviour
    3. {
    4.     void Start()
    5.     {
    6.         StartCoroutine(MyCounter(10));
    7.     }
    8.    
    9.     IEnumerator MyCounter(int number)
    10.     {
    11.         int i = 0;
    12.         while(i < number)
    13.         {
    14.             //do stuff
    15.             yield return 0; //Wait 1 Frame
    16.             //yield return new WaitForSeconds(1.0f) //wait 1 second per interval
    17.         }
    18.     }
    19. }
    20.  
    If you were to pass in a delegate to this function you could then create an invoke X times function where you can reuse it for any value of X or any function with the same signature.
     
  3. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,400
    Code (csharp):
    1. private var counter : int;
    2.  
    3. function Start () {
    4.     counter = 10;
    5.     InvokeRepeating("Foo", 0, 0);
    6. }
    7.  
    8. function Foo () {
    9.     // do stuff
    10.     if (--counter == 0) CancelInvoke("Foo");
    11. }
    I guess that's what you meant, but there's nothing particularly wrong with that.

    --Eric
     
    muhammedaltun and tkamruzzaman like this.
  4. BlackMantis

    BlackMantis

    Joined:
    Feb 7, 2010
    Posts:
    1,475
    Hey that's cool, I didn't know you could put the operator on the left side and check it in the same statement. I don't know why there isn't just a fourth parameter in InvokeRepeating. LOL
     
  5. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,400
    You can do "if (counter-- == 0)" too, but that's somewhat different. The first way decrements the number immediately and compares the result, while the second way compares the variable to the result and then decrements it.

    --Eric
     
  6. danny1111

    danny1111

    Joined:
    Nov 24, 2015
    Posts:
    5
    I know this is an old thread but I just wanted to point out that you missed out the increment of i at the end so that would be an infinite loop.
     
  7. NCartist

    NCartist

    Joined:
    Nov 3, 2013
    Posts:
    15
    Could you please post the correct update that you are suggesting?
     
  8. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    He meant that "i++" was missing from within the loop, in order for it to break out at the condition. :)
     
  9. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    or just use a for loop.
     
    Ottoth likes this.
  10. danny1111

    danny1111

    Joined:
    Nov 24, 2015
    Posts:
    5
    Code (CSharp):
    1.  
    2. public class MyClass : MonoBehaviour
    3. {
    4.     void Start()
    5.     {
    6.         StartCoroutine(MyCounter(10));
    7.     }
    8.  
    9.     IEnumerator MyCounter(int number)
    10.     {
    11.         int i = 0;
    12.         while(i < number)
    13.         {
    14.             //do stuff
    15.             yield return 0; //Wait 1 Frame
    16.             //yield return new WaitForSeconds(1.0f) //wait 1 second per interval
    17.             i++;
    18.         }
    19.     }
    20. }
    21.  
    All you need is to increment the variable i each iteration.
     
    dyupa likes this.