- Home /
I'm trying to make a specific enemy object spawn at one spawner while another enemy type will spawn at the other spawns
I currently have three spawners, the spawners all have the ability to spawn enemies. These enemies will spawn in a row that moves towards the player. One random spawner will spawn a power up ability though instead of an enemy. I only want one to spawn for each row spawned, as well as spawning in different spawners each time. The way i have it set up now is that each spawner has its own script with a random static int that determines whether to spawn an enemy or a power up. The code is below
EnemySpawner
public GameObject[] enemies;
public Vector3 spawnValues;
public float spawnWait;
public bool stop;
private Rigidbody rb;
public static int enemy;
// Start is called before the first frame update
void Start()
{
StartCoroutine(spawner());
}
// Update is called once per frame
void Update()
{
spawnWait = 5.0f;
}
IEnumerator spawner()
{
yield return new WaitForSeconds(spawnWait);
while (!stop)
{
enemy = Random.Range(0, 2);
Vector3 spawnPosition = new Vector3(Random.Range(-spawnValues.x, spawnValues.x), 0.0f, spawnValues.z);
Instantiate(enemies[enemy], spawnPosition + transform.TransformPoint(0, 0, 0), gameObject.transform.rotation);
yield return new WaitForSeconds(spawnWait);
}
}
}
I also have a master spawner that takes the (enemy) random value from each spawner script and checks its value. If the value of enemy is 1(powerup) it is supposed to set the value of enemy in the other scripts to 0(enemy). MasterSpawner
void Update()
{
enemyNum1 = SpawnEnemy.enemy;
enemyNum2 = SpawnEnemy2.enemy;
enemyNum3 = SpawnEnemy3.enemy;
if(enemyNum1 == 1)
{
enemyNum2 = 0;
enemyNum3 = 0;
}
if(enemyNum2 == 1)
{
enemyNum1 = 0;
enemyNum3 = 0;
}
if(enemyNum3 == 1)
{
enemyNum2 = 0;
enemyNum1 = 0;
}
}
I understand this is definitely not the best way to do this. Any help is appreciated.