Spawning Ammo

Okay I have tried to research this using both the scripting reference and on Unity Answers but I guess I am having troubles understanding the Instantiate function with my random ammo spawning script.

I am wanting to spawn a random ammo kit at a specific spawn point. I am setting up the variables or instantiate right? I am a compete noob when it comes to scripting.

var ammo = 0;

var pistolAmmo = GameObject;

var rifleAmmo = GameObject;

var shotgunAmmo = GameObject;

var assaultAmmo = GameObject;
var x = 0;

var y = 0;

var z = 0;

function Start () {
AmmoSpawn();
}

function AmmoSpawn() {

ammo = Random.Range(1, 4);

switch(ammo)
{

	case 1: //Spawns Pistol Ammo
		
		 Instantiate (pistolAmmo, Vector3(x, y, z), Quaternion.identity);
		
		break;
	
	case 2: //Spawns Rifle Ammo
		
		Instantiate(rifleAmmo, Vector3(x, y, z), Quaternion.identity);
	
		break;
	
	case 3: //Spawns shotgun Ammo
		
		Instantiate(shotgunAmmo, Vector3(x, y, z), Quaternion.identity);
	
		break;
	
	default: //Spawns assault Rifle Ammo
		
		Instantiate(assaultAmmo, Vector3(x, y, z), Quaternion.identity);
	
		

}

}

You could try do this by spawning at certain place. You might need to create new tags by going into tag manager.

var ammoBox : Transform;
var spawnpoint : Transform;

function Update () {
//Here, the if statement means if the spawnpoint has a "empty" tag
// It will Instantiate an ammo box where the spawnpoint is
if (spawnpoint.CompareTag("Empty")) {
Instantiate(ammoBox, spawnpoint.transform, spawnpoint.rotation);
spawnpoint.Tag = ("used");
}
//If the spawnpoint has a "used" tag
//It will not do anything or you could put some actions
else if (spawnpoint.CompareTag("used")) {
//do nothing or you could choose
}
}

In your script you have errors in the declarations of your ammo box types e.g. “var pistolAmmo = GameObject;”. This should generate a syntax error. If you want to specify the type of a variable use the syntax “var pistolAmmo : GameObject;” instead (use : instead of =). After that be sure to set pistolAmmo etc. inside the editor to their proper values.

Other than that your script should work as intended unless I’ve missed something. :stuck_out_tongue: