- Home /
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);
}
}
from a quick glance at the code, it looks like it should be possible. it's dependent on the type of object that you put in the enemy variable in the Waves
array. are you populating that in the inspector?
you'd have to do something else if you wanted multiple types spawned in a single wave...
Thank you for your reply could you point me to the direction were i could make such a code that would add more tipes of enemys but whit wave tipen of spawner like this one
Answer by joshua-lyness · Jul 15, 2016 at 12:31 AM
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. :)
thank you for your reply i changed the public Transform ;
to
pubic GameObject [] enemy ;
added the lines as you wrote but i got a bunch load of erros and here
Instantiate (enemy[randomInt], sp.position, sp.rotation);
you missed one of the ")" so i added in the end but still bunch of errors :(((
Assets/Scripts/$$anonymous$$anagers/wawespawner.cs(108,25): error CS1503: Argument #1' cannot convert
UnityEngine.GameObject[]' expression to type `UnityEngine.Transform'
If you look at the error, it has (108,25) in it. This means the 25th character on line 108 is the error, nothing to do with the instantiate. Its saying it cant convert GameObject to Transform, which if you look on that line, youre trying to pass a GameObject array into the function. If you follow that line down to where the function is declared, you can see the parameter is of type Transform, yet youre trying to put a GameObject[] in. Change it to GameObject[] enemy and itll work.
well i still get errors ...iam terible at scripting iam beter at 3d modeling but iam tired of just making models i whant to add em to a game and iam sorry to bother you but could you help me out i know iam a pain in the a** but whats wrong whit my code this is the changed version using UnityEngine; using System.Collections;
public class wawespawner : $$anonymous$$onoBehaviour
{
public enum SpawState {Spawning, waiting, counting };
[System.Serializable]
public class Wave
{
public string name;
public GameObject [] 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 ("SPaw$$anonymous$$g 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 (GameObject enemy)
{
Debug.Log ("SPawning enemy" + enemy.name);
Transform sp = spawnpoints [Random.Range (0, spawnpoints.Length)];
int randomInt = Random.Range(0, 3);
Instantiate (enemy[randomInt], sp.position, sp.rotation);
}
}