make Update display/animate iterations of While(i<20)loop?

it’s a digger function that digs tunnels in space when i press z button.

its in a while loop so if i want to dig 20 spaces, i do the while loop 20 times. this freezes unity for a moment, and then shows all 20 spaces have been dug.

I would like to see the tunnel advancing as fast as the processor can dig, i dont mind scrapping the while loop for something else, or to include a command “RefreshDisplay” in the while loop, something like that? thanks for being so amazingly helpful guys i am learning so much :wink:

function Update()
{
  if ( Input.GetKeyDown("z") )
  {
     d=0;
     while (d < digSize)
  {

  DigRandomTunnel();

   d=d+1; }
}
}

You can try something like this:

    function Update()
    {
       if(Input.GetKeyDown("z"))
       {
         digsRemaining += digsPerPressedZ;
    
         if(digsRemaining > digsMax)
         {
           digsRemaining = digsMax;
         }
       }
    
       if(digsRemaining > 0)
       {
          DigRandomTunnel();
          digsRemaining = digsRemaining - 1;
       }
    }

This way each time the key is pressed you only increase the counter (with an optional limit). Each frame DigRandomTunnel() will be called only once so that the screen is updated as quickly as possible and the process doesn’t get stalled. This will happen until there is nothing left to dig (digsRemaining will be 0).

It would be helpful to know what you are doing in the DigRandomTunnel() function, but I think that a while loop is not what you would need in this situation. The while loop will try to execute the 20 spaces in one single frame…

How are you specifying digSize’s value though? Does the user input the value? Or would you like the digSize to vary depending on the duration the user presses the z key?

If you could give us the code in your DigRandomTunnel() function it would be easier to how exactly you’re making the blocks disappear in the game…
This code is assuming you are using an animation and that you would like the amount of spaces dug to depend upon the duration of the keypress:


function Update()
{
  if ( Input.GetKeyDown("z") )
  {
    DigRandomTunnel();          //call DigRandomTunnel() once every frame
  {
}

function DigRandomTunnel()
{
  digSize++;                  //increase digSize each time we remove a block
  yield WaitForSeconds(0.2);  //wait for 0.2 sec before next block is removed

  animation.Play();             //play animation or whatever it is you are doing here
}

This is just a guess at what the code might look like, however to be able to actually give a proper answer we would have to see some more of your code :slight_smile: