I’ve currently got a script that spawns enemy GameObjects from an array at random. What I wanted to do was spawn a special enemy at a specified rate, for example every 7 seconds. I created a timer and a second Instantiate but it isn’t working and the special enemy isn’t spawning at all.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemySpawnerScriptt : MonoBehaviour
{
public GameObject enemy;
public GameObject enemy2;
public GameObject enemy3;
public GameObject enemy4;
public GameObject enemy5;
public GameObject enemy6;
public GameObject enemy7;
float randX;
Vector2 whereToSpawn;
public float spawnRate = 2f;
float nextSpawn = 0.0f;
float timer = 0f;
bool started = false;
public GameObject startCanvas;
public GameObject scoreCanvas;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0) == true)
{
started = true;
startCanvas.SetActive(false);
scoreCanvas.SetActive(true);
}
if ((Time.time > nextSpawn) && started)
{
nextSpawn = Time.time + spawnRate;
randX = Random.Range(-2.4f, 2.4f);
GameObject[] enemyArray = { enemy, enemy2, enemy3, enemy4, enemy5, enemy6};
int randEnemy = UnityEngine.Random.Range(0, enemyArray.Length);
whereToSpawn = new Vector2(randX, transform.position.y);
Instantiate(enemyArray[randEnemy], whereToSpawn, Quaternion.identity);
if(timer < 7f)
{
timer += Time.deltaTime;
}
else
{
timer = 0;
Instantiate(enemy7, whereToSpawn, Quaternion.identity);
}
}
}
}