AI isn't looping (Javascript)

Hi! So I’m scripting the AI for the final boss in my game, and I’m having an issue with the script not looping properly. When the player enters a certain distance from the boss, he sets the “state” variable of the boss from 0 to 1. When the state is 1, the boss performs his function, and when he finishes he sets the state to 2. When the state is 2, the boss performs the function for that state, and sets it to 3. etc.
Once the boss gets to state 6 (the final state), he does his function then sets the state back to 1 so it will repeat. But once it sets the state back to 1, the boss doesn’t do anything. Why is this happening?

var state : int;

//To trigger the boss battle
function OnTriggerEnter (other : Collider)
{
	if(other.gameObject.tag == "shootAtMe")
	{
		state = 1;
//step one
		if (state == 1)
	{
		Shoot();
		yield WaitForSeconds(4);
		state = 2;
	}
//step two
	if (state == 2)
	{
		MoveLeft();
		if (transform.position.x < -10)
		{
			state = 3;
		}
	}
//step three
	if (state ==3)
	{
		Shoot();
		yield WaitForSeconds(4);
		state = 4;
	}
//step four
	if (state ==4)
	{
		MoveRight();
		yield WaitForSeconds(4);
		state = 5;
	}
//step five
	if (state == 5)
	{
		Shoot();
		yield WaitForSeconds(4);
		state = 6;
	}
//step six
	if (state ==6)
	{
		MoveLeft();
		yield WaitForSeconds(2);
		moveSpeed = 0;
		state = 1;
	}
	
	}
}

The boss will wait for another OnTriggerEnter to repeat the sequence - if you exit and reenter the trigger it should work.

But you don’t need these states - coroutines are great to implement state machines: just execute the instructions in sequence. WaitForSeconds will pause the coroutine for the desired intervals; the only weird thing is state 2, where it only passes to state 3 if the x coordinate is lower than -10; do you want it to repeat MoveLeft() until x < -10? Assuming that’s the case, your AI could become something like this:

private var inRange = false; // tells when the player is inside range
private var executing = false; // tells when the boss is executing its cycle

function OnTriggerEnter (other : Collider){
    if(other.gameObject.tag == "shootAtMe"){
        inRange = true; // player entered the trigger
        if (!executing) ExecuteBossCycle(); // if boss cycle not already executing, start it
    }
}

function OnTriggerExit(other : Collider){
    if(other.gameObject.tag == "shootAtMe"){
        inRange = false; // player exited trigger - don't repeat cycle
    }
}

function ExecuteBossCycle(){
    executing = true; // cycle is executing
    while (inRange){  // repeat cycle while player in range
        //step one
        Shoot();
        yield WaitForSeconds(4);
        //step two - don't know if this was what you're trying to do:
        while (transform.position.x >= -10){
            MoveLeft(); // call MoveLeft while boss position.x >= -10
            yield; // let Unity free till next frame
        }
        //step three
        Shoot();
        yield WaitForSeconds(4);
        //step four
        MoveRight();
        yield WaitForSeconds(4);
        //step five
        Shoot();
        yield WaitForSeconds(4);
        //step six
        MoveLeft();
        yield WaitForSeconds(2);
        moveSpeed = 0;
        // will repeat cycle if player inside range
    }
    executing = false; // cycle ended
}