- Home /
Need help with random spawning system.
Ok, so im a new programmer, so my code is a little messy. but i will give you what i have completed. What I am hoping to accomplish is: 1. Create a wave spawn system with slight delay between spawns so enemy prefabs dont stack up and controlled wave delay between waves. 2. Randomize which empty game object (spawn point) is used to instantiate the prefabs. there will be four separate points. 3. Spawn specific enemies, not random, for each wave. So basically, i would have the ability to declare which enemy prefabs spawn on which waves with not pattern at all. (I understand this part will be time consuming.)
using UnityEngine;
using System.Collections;
public class SpawnForWave : MonoBehaviour
{
public int WaveCount = 1;
private int EnemyCount = 0;
private int Ammount2Spawn = 10;
private GameObject SpawnPoints = GameObject.FindGameObjectWithTag("Spawn Point");
public GameObject EnemyPref1;
public GameObject EnemyPref2;
public GameObject EnemyPref3;
public GameObject EnemyPref4;
void Update ()
{
if(WaveCount = 1)
{
for(int i = 0; i < Ammount2Spawn; i++)
{
Instantiate(EnemyPref1, SpawnPoints.position, SpawnPoints.rotation);
Instantiate(EnemyPref2, SpawnPoints.position, SpawnPoints.rotation);
Instantiate(EnemyPref3, SpawnPoints.position, SpawnPoints.rotation);
Instantiate(EnemyPref4, SpawnPoints.position, SpawnPoints.rotation);
}
}
}
}
So umm... what's your problem? You need to ask a specific question, we are not here to simply write a script to your specification!
Don't know how to delay a wave spawn?
check out WaitForSeconds
Also see the page about coroutines
How to choose your spawn point?
How to pick which enemies to spawn?
Personally I would set up a list of lists here (search for 2D array). Then your first index relates to which level it should be used by, and your second index is your prefab for the enemy you need to spawn.
Spawn them with a for loop, which you seem to know how to do!
Scribe
Answer by RedDevil · Aug 18, 2014 at 08:30 AM
I dont recomand using this for spawn points:
private GameObject SpawnPoints = GameObject.FindGameObjectWithTag("Spawn Point");
If you do this then it will have to search for it everytime. Just declare it as public and add it in the inspector.
Create a wave spawn system with slight delay between spawns so enemy prefabs dont stack up and controlled wave delay between waves. To do this you must create a new IEnumerator class and add wait for second calls like this: Just an example:
IEnumerator example() { Instantiate(EnemyPref1, SpawnPoints.position, SpawnPoints.rotation); yield return new WaitForSeconds(Random.Range(1,5)); }
To call this function do it like this:
StartCoroutine(example());
2.Randomize which empty game object (spawn point) is used to instantiate the prefabs. there will be four separate points. You can create a start a random number then asign with if statements for a value to spawn at a prefab.
3.Spawn specific enemies, not random, for each wave. So basically, i would have the ability to declare which enemy prefabs spawn on which waves with not pattern at all. (I understand this part will be time consuming.) You can just make a whole new function and with yields and timers spawn them just like that.
nice answer +1 :)
GameObject.FindGameObjectWithTag("Spawn Point");
will only 'have to search for it everytime' if you put it in a repeating method like Update. If it is called in Start it should work fine without having to be called each time, though your alternative would work fine as well!
so puttin it there is the alternative of start. I see thank you i did not know that.
Your answer
![](https://koobas.hobune.stream/wayback/20220613160846im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
When does Instantiate return ? 2 Answers
Spawn Random Enemy 3 Answers
instantiating vertically 2 Answers
How to Spawn multiple game object one by one in random order 1 Answer
Spawn GameObjects without overlap 1 Answer