Issue With Spawning Enemies (javascript)

This is my first project on Unity so I am learning as I go. I am creating a 2d scroller game and I’m at the point where I need to spawn the enemys. I have three enemys named: car, semi and bike. I have the logic of when the enemy will spawn and what enemy it will be. (I just have it print to the console of what vehicle it will be and what lane it will be in)

function enemyFunction () : IEnumerator {
	var lane: int = Random.Range(1,4);
	var vehicleType: int = Random.Range(1,7);
	switch (vehicleType){
		case 1: //Semi
		case 2: 
			switch (lane){
				case 1: 
					Debug.Log("Semi, lane 1");
					yield WaitForSeconds (3);
                    enemyFunction();
  				break;
				case 2:
					Debug.Log("Semi, lane 2");
					yield WaitForSeconds (3);
					enemyFunction();
				break;
				case 3:
					Debug.Log("Semi, lane 3");
					yield WaitForSeconds (3);
					enemyFunction();
				break;			
			}
		break;
		case 3: //bike
			switch (lane){
				case 1: 
					Debug.Log("Bike, lane 1");
					yield WaitForSeconds (3);
					enemyFunction();
				break;
				case 2:
					Debug.Log("Bike, lane 2");
					yield WaitForSeconds (3);
					enemyFunction();
				break;
				case 3:
					Debug.Log("Bike, lane 3");
					yield WaitForSeconds (3);
					enemyFunction();
				break;			
			}
		break;
		case 4: //car
		case 5:
		case 6:
			switch (lane){
				case 1: 
					Debug.Log("Car, lane 1");
					yield WaitForSeconds (3);
					enemyFunction();
				break;
				case 2:
					Debug.Log("Car, lane 2");
					yield WaitForSeconds (3);
					enemyFunction();
				break;
				case 3:
					Debug.Log("Car, lane 3");
					yield WaitForSeconds (3);
					enemyFunction();
				break;			
			}
		break;			
	} 
}	
	
enemyFunction();

I have this written on a master script that is connected to an empty game object. Would I want to have the actual code of spawning in this script, or would I have something that would refer to another script that is attached to the actual vehicle that I am spawning. I am completely lost on how to do this.

You need these variables, which you fill in the Inspector;

var bike : GameObject;
var car : GameObject;
var semi : GameObject;

var lane1 : Vector3;
var lane2 : Vector3;
var lane3 : Vector3;

and this function:

function Spawn (what : GameObject, where : Vector3) {
    var vehicle : GameObject = Instantiate (what, where, Quaternion.identity);
    }

Then you would just replace all those Debug messages with (for example):

Spawn (car, lane2);

Btw, that’s some fugly logic you’ve got going on. Who taught you to write recursive functions with nested switch/case statements anyhow? Look up InvokeRepeating, you can rewrite the whole thing much more neatly with that.

A typical kind of thing to do with this is make this script have public variables that store the prefabs you want to spawn. You can make a prefab of the car for example, and then store that in a GameObject variable:

var car : GameObject;

Then you can run the Instantiate function on that prefab to create the car. This script could call it, you wouldn’t want to try to have that spawning script be attached to the enemy, as an enemy cannot create itself if it doesn’t exist yet :wink:

The instantiate documentation is here:

And here is the example they give:
Instantiate (prefab, Vector3(i * 2.0, 0, 0), Quaternion.identity);

So prefab would be your car variable, the Vector3 is the position you want it to create at, which you can set by getting another game object’s position if you have a spawner, and the last part is the rotation of the spawned prefab.

If any of this needs clarification let me know!