Trying to instantiate random enemy prefab from array

I’ve been teaching myself how to use Unity for the past month or so now. I originally wrote my Enemy Spawner script with hard coded values, and decided to clean and optimize it by using a coroutine and an array. I’ve now hit a snag with the instantiate.

I know WHAT I need to do, but I’m still unfamiliar with Unity.

function SpawnRandomEnemy()
    //var EnemyPrefab = ChooseRandomPrefab();
    var RandomIndex = Random.Range(0, EnemyDroneArrayLength);
    Instantiate(EnemyDroneArray[RandomIndex, transform.position, Quaternion.identity]);
    //Enemy_Drone_1.Instantiate(Enemy_Drone_1, transform.position, Quaternion.identity);
    //Enemy_Drone_2.Instantiate(Enemy_Drone_2, transform.position, Quaternion.identity);


I’ve left the commented lines in to show what I’ve been trying. I know that those last 2 lines are not what I want, however they are, in a sense. I know that I want to randomly spawn either enemy 1 or enemy 2 (as I add more basic enemies, this would go up). The last 2 lines will cause both enemies to spawn at the same time, instead of one or the other. With this snippet of code, however, I am receiving this error:

Assets/Main Scene/Scripts/Enemy_Spawn_Script.js(69,32): BCE0017: The best overload for the method 'Array.get_Item(int)' is not compatible with the argument list '(int, UnityEngine.Vector3, UnityEngine.Quaternion)'.

I understand that RandomIndex is an int, I’m just not sure what I am overlooking here. Probably something so simple that the answer is staring me in the face, and I’ll slap myself once I realize what it is.

The entire script can be found here: #pragma strictvar Direction : int = 1;var speed : int = 5;var Enemy_Dr - The snippet is located between lines 65 - 73.

Edit: To give a better idea, I have a GameObject “Enemy Spawner” that bounces left and right constantly, and will randomly spawn an enemy at a random interval. The enemies just fall down from the top of the screen to the bottom.

If You make a boundary and attach this script to like a cube game object without the mesh rendered then you can attach prefabs to the scripts which spawn randomly on the y axis in the boundary hope this helped.

using UnityEngine;
using System.Collections;

public class EnemySpawnerController : MonoBehaviour {

	private Collider collider;

	public int hazardCount;
	//public int hazardCountMax;
	//public int hazardCountMin;

	public int WaveWait;
	//public int WaveWaitMin;
	//public int WaveWaitMax;

	//float WaitMax =10f;
	//float WaitMin = 5f;

	public float spawnWait;

	public float SpawnValueX;
	public GameObject[] EnemyPrefabs;
	void Start () 
		collider = GetComponent<Collider> () as Collider;
		StartCoroutine (SpawnWaves ());

	private Vector3 MakeRandomSpawnPosition()
		Vector3 local = collider.transform.position;
		local.x = SpawnValueX - (collider.transform.localScale.x/2);
		local.y = Random.Range(local.y, local.y + collider.transform.localScale.y) - (collider.transform.localScale.y /2);
		local.z = 0f;//Random.Range(local.z, local.z + collider.transform.localScale.z) - (collider.transform.localScale.z /2);
		return local;

	IEnumerator SpawnWaves ()
		float currentWait = 1f;//Random.Range (WaitMin, WaitMax);
		//WaveWait = 2;//Random.Range (WaveWaitMin, WaveWaitMax);
		//hazardCount = 3;// Random.Range (hazardCountMin, hazardCountMax);

		yield return new WaitForSeconds (currentWait);
		while (true)

			for (int i = 0; i < hazardCount; i++)
				Quaternion spawnRotation = Quaternion.identity;
				GameObject gameObjectInstance = MakeRandomGameObject(MakeRandomSpawnPosition());
				//Instantiate (enemyPrefab, spawnPosition, spawnRotation);
				yield return new WaitForSeconds (spawnWait);
			yield return new WaitForSeconds (WaveWait);

	private GameObject MakeRandomGameObject(Vector3 position)
		GameObject enemyPrefab = EnemyPrefabs[Random.Range(0, EnemyPrefabs.Length)];
		Vector3 spawnPosition = MakeRandomSpawnPosition();	
		Quaternion spawnRotation = Quaternion.identity;
		GameObject gameObjectInstance = Instantiate(enemyPrefab, spawnPosition, Quaternion.identity) as GameObject;
		return gameObjectInstance;

	void OnTriggerExit (Collider other)
		if (other.gameObject.tag == "Enemy") 
			Destroy (other.gameObject);
		else if(other.gameObject.tag == "PickUp")
			Destroy (other.gameObject);

Instantiate(EnemyDroneArray[RandomIndex], transform.position, Quaternion.identity);

The syntax is array[index], the other two are parameters for the Instantiate function and shouldn’t have anything to do with the array.

It would be good to move the length variable assignment into Start to make sure the Array has been initialised before you check its length.

I also recommend you stick to convention of beginning variable names with a lower-case letter, in order to differentiate them from properties and methods/functions.

You can declare your array like this and just drag the prefabs into the array in the inspector:

#pragma strict

public var enemyDrones : Transform[];
private var enemyDronesLength : int;

function Start () {
	enemyDronesLength = enemyDrones.Length;