Pause a for loop in C#

Hello,

I have a function in C# which is executed from another script (once).
I want to pause the for loop in that function for a second each time it loops.

I don’t use any updates in my code.

public void ExplodeAll ()
{
    for (int x = 0; x < width; x++) 
    {
       for (int y = 0; y < height; y++)
       {
	  if (test[x, y].GetComponentInChildren<Blokje> ().isMine) 
          {				
              test[x, y].GetComponentInChildren<Blokje> ().Explode ();
	  }
       }
    }
    Win ();
}

I want to explode the cubes (blokje) after each other instead of all at once.

Answer by Zine92

public void ExplodeAll()
{
    StartCoroutine("Explode");
}

IEnumerator Explode()
{
    for (int x = 0; x < width; x++)
    {
        for (int y = 0; y < height; y++)
        {
            if (test[x, y].GetComponentInChildren < Blokje > ().isMine)
            {
                test[x, y].GetComponentInChildren < Blokje > ().Explode();
                yield return new WaitForSeconds(1.0f);
            }
        }
    }
    Win();
    yield return null;
}

Thanks

you can use a coroutine and yield for a certain time

thanks apples, i saw that in another topic where you posted some example code. but i don’t know how to implement it in my code. could you give me a start?

I don’t use a update only some methods like the above one.

thanks,

It’s allways a good idea to have a look in the script reference, Unity even provide examples :
http://unity3d.com/support/documentation/ScriptReference/MonoBehaviour.StartCoroutine.html

of course i tried that. The problem is that i don’t know how to pause that for loop. for example. i have a button that executes a for loop which prints a text to the debug log 10 times.

I want to wait a second between a loop. Which code and which functions do i have to add to my code?

public void ExplodeAll()
{
    StartCoroutine("Explode");
}

IEnumerator Explode()
{
    for (int x = 0; x < width; x++)
    {
        for (int y = 0; y < height; y++)
        {
            if (test[x, y].GetComponentInChildren < Blokje > ().isMine)
            {
                test[x, y].GetComponentInChildren < Blokje > ().Explode();
                yield return new WaitForSeconds(1.0f);
            }
        }
    }
    Win();
    yield return null;
}

Try this. I haven’t bug tested it yet but it should work.

You are amazing zine92 . i finally understand co routines in C#

I will post the answer in my main post with your name.

Thanks again

Nope. You did the right thing, asked the right question and i helped. :smile: And actually i deal with co-routines and C# recently and i realized co-routines really work very differently in js and in c#. Have fun. :smile:

And yeah thanks for having my name there. :smile:D

You don’t actually need the
yield return null;
at the end

Just a minor point.

Really? I am using visual studio and it will underline my function saying not all path return a IEnumerator. Therefore it’s a habit for me now. :smile:

Monodevelop on a mac also says not all paths return.

Just tested the exact code in visual studios 2010 without the null return and works fine.

It only returns a “not all path return a IEnumerator” error if there is no yield return at all.

Interesting, maybe older versions require it.