Scripting help

Hello dear coders i need your help i made a code whit the help of youtube and i need help the code works perfectly it spaws enemys and waves waits for me to kill all the enemys then starts a new wave but there is one problem whit the code it onky alows me to use one tipe of prefab (enemy) how can i modify the code to spaw multiple tipes of enemys
this is the code

using UnityEngine;
using System.Collections;

public class wawespawner : MonoBehaviour 
{
	public enum SpawState {Spawning, waiting, counting }; 

	[System.Serializable]
	public class Wave
	{
		public string name; 
		public Transform enemy;
		public int count;
		public float rate;
	}

	public Wave [] Waves;
	private int nextWave = 0;

	public Transform[] spawnpoints;

	public float TimeBetweenWaves = 5f;
	private float wawecountdown;

	private float Searchcountdown = 1f;

	private SpawState state = SpawState.counting;

	void Start ()
	{
		if (spawnpoints.Length == 0)
		{
			Debug.LogError ("We need SpawPoints");
		}


		wawecountdown = TimeBetweenWaves;
	}

	void Update ()
	{
		if (state == SpawState.waiting) 
		{
			if (!Enemyisalive ()) 
			{
				WaveComploted ();
			} 
			else
			{
				return;
			}
		}
		if (wawecountdown <= 0) 
		{
			if (state != SpawState.Spawning)
			{
				StartCoroutine (SpawWave (Waves [nextWave]));
			}
		}
		else 
		{
			wawecountdown -= Time.deltaTime;	
		}
	}


	void WaveComploted ()
	{
		Debug.Log ("WaveCompleted");

		state = SpawState.counting;
		wawecountdown = TimeBetweenWaves;
		if (nextWave + 1 > Waves.Length - 1) {
			nextWave = 0;
			Debug.Log ("We comploted all waves");
		} 
		else
		{
			nextWave++;
		}


	}


	bool Enemyisalive ()
	{
		Searchcountdown -= Time.deltaTime;
		if ( Searchcountdown <= 0f)
		{
			Searchcountdown = 1f;
			if (GameObject.FindGameObjectWithTag ("Enemy") == null)
			{
				return false;
			}

		}
		return true;
	}

	IEnumerator SpawWave (Wave wave)
	{
		Debug.Log ("SPawming wawe" + wave.name); 
		state = SpawState.Spawning;

		for (int i = 0; i < wave.count; i++) 
		{
			SpawEnemy (wave.enemy);
			yield return new WaitForSeconds (1 / wave.rate);
		}

		state = SpawState.waiting;
		yield break; 
	}
	void  SpawEnemy (Transform enemy)
	{
		Debug.Log ("SPawning enemy" + enemy.name);

		Transform sp = spawnpoints [Random.Range (0, spawnpoints.Length)];
		Instantiate (enemy, sp.position, sp.rotation);

	}
}

You want to use an array. An array is like a list of variables. To declare an array, you want to replace

public Transform enemies;

with :

public Transform[] enemies;

Now in the inspector, you drag and drop the enemies GameObjects into the enemies variable. You most likely don’t want them to be Transform anyway, you probably want GameObject.


Now you want to randomly choose enemies in this array to spawn. To do this, it’s fairly simple. Above your line :

Instantiate (enemy, sp.position, sp.rotation);

you want to write this :

int randomInt = Random.Range(0, *length of your array of enemies*);

What this does is before it instantiates your enemy, it makes a random number between the range your specify. Since we have an array of enemies, we need to change the instantiate() line to use the randomInt to choose an enemy. Replace the Instantiate() line with this :

Instantiate (enemy[randomInt], sp.position, sp.rotation);

That will choose from your list of enemies, and instantiate any random one of them. Hope this helped, have a good day!


Just a little tip, make sure you are more specific with your title, make it something like “randomly select GameObjects,” since people are more likely to notice it. :slight_smile: