Spawning enemies in waves

Hi,

I am trying to make a spawn system in waves. My spawn is working but the wave section is not.

Once one enemy dies, enemies are just randomly spawn out of control

var location:Transform;
var enemySelect:Rigidbody2D;
var spawn1:Transform;
var spawn2:Transform;
var spawn3:Transform;
var waveTime:float;
var waveLevel:int = 1;
var totalEnemy:int = 2;
var enemyAlive:int = 0;
var waitToWave:float = 10;
var waveMultiplier:int = 2;
var enemy1:Rigidbody2D;
var enemy2:Rigidbody2D;
var enemy3:Rigidbody2D;


function Awake () 
{
	Spawn();
	//enemyCount = GameObject.FindGameObjectsWithTag("Enemy");
//var tempobj : GameObject = GameObject.FindWithTag("Enemy");
var countScript : EnemyHealth = GetComponent(EnemyHealth);

}

        	



function FixedUpdate () {


      
     
    if (enemyAlive == 0) { 
        //the is no creature left; create a wave!
        waveLevel++;
        totalEnemy = totalEnemy * waveMultiplier;
        Spawn();
        }
        
        if (EnemyHealth.health == 0){
        enemyAlive--;
       
     Debug.Log("enemy died");
     }
     }
     
        
    
    
	
    if (waitToWave <= waveTime) {
        waveTime = 0; 
        //reset time
        //enemyAlive = totalEnemy;
        
 	}
 	
 	

	
	
			function Spawn() 
	{
  
	 for(var i:int=0; i<totalEnemy; i++) {
	        enemyAlive++;
	 //select a random number, inside a maths function absolute command to ensure it is a whole number
	 var randomPick:int = Mathf.Abs(Random.Range(1,3));
	 
	 
	 
	 //create a location 'Transform' type variable to store one of 3 possible locations declared at top of script
	 var location1:Transform;
	 location1 = spawn1;
	 
	 var location2:Transform;
	 location2 = spawn2;
	 
	 var location3:Transform;
	 location3 = spawn3;
	 
	 if(randomPick == 1){
	  location = location1;
	  Debug.Log("Choose loc 1");
	 }
	 else if(randomPick == 2){
	  location = location2;
	  Debug.Log("Choose loc 2");
	 }
	 else if(randomPick == 3){
	  location = location3;
	  Debug.Log("Choose loc 3");
	 }
	 //select a random number, inside a maths function absolute command to ensure it is a whole number
	 var randomPickEnemy:int = Mathf.Abs(Random.Range(1,4));
	 

	 //check what randomPick is, and select one of the 3 locations, based on that number
	 if(randomPickEnemy == 1){
	  enemySelect = enemy1;
	  Debug.Log("Choose enemy 1");
	 }
	 else if(randomPickEnemy == 2){
	  enemySelect = enemy2;
	  Debug.Log("Choose enemy 2");
	 }
	 else if(randomPickEnemy == 3){
	  enemySelect = enemy3;
	  Debug.Log("Choose enemy 3");
	 }
	 
	 
	 
	 
	 //create the object at point of the location variable
	 var thingToMake:Rigidbody2D;
	 Debug.Log(location);
	 thingToMake = Instantiate(enemySelect, location.position, location.rotation);
	 thingToMake.AddForce(Vector3(0,0,100));
	 
	 
	 //halt script for 1 second before returning to the start of the process
	 yield WaitForSeconds(5);

	}
	}

I have tried for hours trying to move loops around,brackets etc. but all has failed

what am i doing wrong?

I am afraid you are doing it in the wrong way. Calling WaitForSeconds() inside Spawn() which is called inside FixedUpdate() is incorrect. You need a co-routine. I suggest to have a look at the code here : http://unity3d.com/learn/tutorials/projects/space-shooter/spawning-waves Feel free to watch the video, it is an absolutely brilliant tutorial explaining how to spawn enemy waves!