Increment a loop iteration on button press?

you can pause a loop with yeild, how do you pause a loop using a mouse click to increment it?

for (var x = 0; x < 10;)
{

   //Do something;

   if ( Input.GetKey("mouse 1") )
   {
    x+=1;
   }
}

Most traditional programmers (and I am one) forget that gameEngines are a loop. 9/10ths of what you want to write a loop for, doesn’t need one. This is an odd case, since it really does start life as a loop. Original code (I’m guessing):

createStage0 object
for(int i=1;i<end;i++)
  build Stage i assuming current stage is i-1

Converted:

Start() { createStage0 object; i=1; }

Update() if(key press && i<end) { do inside of old loop; i++; }

That’s probably a mess for a nested loop (update will have to manually reset the vars.) I think this would keep it more “in place,” putting in a coroutine to give the ability to delay:

Start() { StartCoroutine( slowLoop() ); }

bool doNext=false;
Update() { if(key press) doNext=true; }

IEnumerator slowLoop() {
  for( ... )
    for( ...)
      while(!doNext) { yield return null; } // <--- delay line
      doNext=false; // <-- clear keypress
      // rest of loop

This is most probably incorrect in some way, but for a positive spin on things :

var isMouseDown : boolean = false;
var isLooping : boolean = false;

function Update()
{
    if ( Input.GetKeyDown("mouse 1") )
    {
        isMouseDown = true;
    }
	else if ( Input.GetKey("mouse 1") )
    {
        isMouseDown = true;
    }
	else if ( Input.GetKeyUp("mouse 1") )
    {
		isMouseDown = false;
    }
	
	if ( !isLooping ) 
	{
		LoopingStuff();
	}
}
   
function LoopingStuff() // function as Update cannot be a coroutine (is that the right terminology?)
{
	isLooping = true;
	
	for (var x = 0; x < 10; x += 0) // I'm a beginner, so wasn't comfortable leaving this empty!
	{

		//Do something;

		if ( isMouseDown )
		{
			x+=1;
		}
		else
		{
			yield;
		}
	}
	
	isLooping = false;
}

This is crashing your game isn’t it? I’m going to tell you the same thing I tell everyone who crashes their game with a loop and doesn’t know why:

Stop using Unity. Go read basic programming books and get a solid foundation of experience under your feet before you go diving into something as complicated as 3D game development.

Only after you can make basic Windows API games will you have enough understanding to get this sort of stuff to work.

For more experienced future researchers getting here from a search engine: Look at coroutines for a solution to this.

Yeah, the loop is going to run through every value of x very quickly, and won’t wait for your mouse click.

Just use a variable in your script (an int, initialized to 0). In your Update() function, check for mouse input, and when it happens, increase your variable and //Do something. Then if x is too big, set it back to 0.