Spawner design for variable spawned units


So I already have a spawner class working fine that spawns a unit based on an instantiation of a single predefined prefab. Great.

Now what I’d like is a spawner that is able to spawn from a selection of unit options, similar to what you see in Pixel Academy or Storm of Vengeance. I’d assume the user would have to define a roster to take with them into the level. Likewise, the enemy spawner would have preselected units as options.

For this to work, what is the best way to reference at runtime the available prefabs? Do I need a class that initially stores a reference to all the default prefab unitd? It would pass the user selected options to the spawner class as and when required? Or is there a more standard way that my inexperience is missing, assuming different unit types are 50+?

If any has a link to anything that I could learn from, that’d be great as I’m not necessarily asking for a coded solution to be provided.


I belive what you’re looking for is what’s called an “Array” or “Object Array”. Heres an example from one of my scripts I use to spawn coins on my 2d side-scroller project;

        // This is the Array game object. The [] creates an array
        public GameObject[] coins;

	private float coinSpawnTimer = 7.0f;

	void start () {

	// Update is called once per frame
	void Update () {
		player = GameObject.FindGameObjectWithTag ("Player");
                coinSpawnTimer -= Time.deltaTime;
		if(coinSpawnTimer < 0.01) {
void SpawnCoins() {
		Instantiate (coins [(Random.Range (0, coins.Length))], new Vector3 (player.transform.position.x + 30, Random.Range (0, 3), 0), Quaternion.identity);	
		coinSpawnTimer = Random.Range (2.0f, 4.0f);

// The coins[(Random,Range (0, coins.Length))] means pick a random object between 0 (first object in array) and the arrays length.

When you assign the script to an object you’ll get a drop down menu for the array, in my case Coins, then you drag and drop the prefabs into the menu, the first object being assined to “0”. Hope this helps