Instantiating at different rates

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);
            }
        }

    }
}

So. I wasn’t sure why this wasn’t working so I copied it and tested it. I used debug.log to find out the value of timer and to find out when things were getting called. The issue here is deltatime is not equal to seconds. The first call of deltatime set the timer to… 0.0040534…

Deltatime is the time in seconds since last frame. Time.time is the time in seconds since the application started.

I made some changes. It spawns a special enemy 7 seconds after the script is started.

{
 
 
     public GameObject enemy;
    public GameObject enemy2;



    float randX;
    Vector2 whereToSpawn;
    public float spawnRate = 2f;
    public float specialSpawnRate = 7f;
    float nextSpawn = 0.0f;
    float timer = Time.time + 7f;
    bool enemySpawned = false;
    bool started = false;



    // Update is called once per frame
    void Update()
    {
        Debug.Log("the current timer value"+timer);

        if (Input.GetMouseButtonDown(0) == true)
        {
            started = true;
        }

        if ((Time.time > nextSpawn) && started)
        {
            nextSpawn = Time.time + spawnRate;

            randX = Random.Range(-2.4f, 2.4f);

            GameObject[] enemyArray = { enemy };

            int randEnemy = UnityEngine.Random.Range(0, enemyArray.Length);

            whereToSpawn = new Vector2(randX, transform.position.y);

            Instantiate(enemyArray[randEnemy], whereToSpawn, Quaternion.identity);


            if (Time.time > timer & enemySpawned == true )
            {
                Debug.Log("timer is increased");
                timer = Time.time + specialSpawnRate;

                Debug.Log("enemy2 should be spawned");
                Instantiate(enemy2, whereToSpawn, Quaternion.identity);
            }

             enemySpawned = true;
        }

    }
}